[英]How to get first image from a tumlbr rss feed in PHP
0Here is the relevant part of my rss feed: 0这是我的rss feed的相关部分:
<channel>
<description></description>
<title>Untitled</title>
<generator>Tumblr (3.0; @xxx)</generator>
<link>http://xxx.tumblr.com/</link>
<item>
<title>Title</title>
<description><figure><img src="https://31.media.tumblr.com/c78c7t3abd23423549d3bb0f705/tumblr_inline_nkp9z234d0uj.jpg"/></figure></description>
<link>http://xxx.tumblr.com/post/99569244093</link>
<guid>http://xxx.tumblr.com/post/99569244093</guid>
<pubDate>Thu, 09 Oct 2014 11:19:33 -0400</pubDate>
</item>
</channel>
Using the answer from other questions on here I tried this: 使用此处其他问题的答案,我尝试了以下方法:
$content = file_get_contents("http://xxx.tumblr.com/rss");
$feed = new SimpleXmlElement($content);
$imgs = $feed->channel->item[0]->description->xpath('//img');
foreach($imgs as $image) {
echo (string)$image['src'];
};
This is returning an empty array for $imgs
这将为$imgs
返回一个空数组
Does it have something to do with the tags being < >
它与< >
标签有关吗< >
< >
etc? 等等?
and if so what can I do? 如果可以,我该怎么办?
You can get it from the description, which seems to include a HTML image tag for the image, by using a simple regular expression with preg_match
: 您可以通过使用带有preg_match
的简单正则表达式,从描述中获得它,该描述似乎包含图像的HTML图像标签:
$content = file_get_contents("http://xxx.tumblr.com/rss");
$feed = new SimpleXmlElement($content);
$img = (string)$feed->channel->item[0]->description;
if (preg_match('/src="(.*?)"/', $img, $matches)) {
$src = $matches[1];
echo "src = $src", PHP_EOL;
}
Output: 输出:
src = http://40.media.tumblr.com/58d24c3009638514325b113859ba369f/tumblr_nk0mwfhKXU1sl87kjo1_500.jpg
Before you can use xapth()
on the description, you need to create a new XML document out of it: 在对说明使用xapth()
之前,需要从中创建一个新的XML文档:
$url = "http://xxx.tumblr.com/rss";
$desc = simplexml_load_file($url)->xpath('//item/description[1]')[0];
$src = simplexml_load_string("<x>$desc</x>")->xpath('//img/@src')[0];
echo $src;
Output: 输出:
http://40.media.tumblr.com/58d24c3009638514325b113859ba369f/tumblr_nk0mwfhKXU1sl87kjo1_500.jpg
I'm not sure if you can use this approach - as already mentioned by kjhughes as comment, your input XML does not contain any img
element. 我不确定您是否可以使用这种方法-正如kjhughes作为注释所提到的那样,您的输入XML不包含任何img
元素。 But it's possible to retrieve the image source using XPath substring-functions: 但是可以使用XPath substring-functions检索图像源:
substring-before(substring-after(substring-after(//item/description[contains(.,'img')],
'src='),'"'),'"')
Result: 结果:
https://31.media.tumblr.com/c78c7t3abd23423549d3bb0f705/tumblr_inline_nkp9z234d0uj.jpg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.