我正在使用此代码从rss新闻Feed导入图片。
$source_xml = simplexml_load_file("http://feeds.bbci.co.uk/news/uk/rss.xml");
foreach ($source_xml->channel->item as $rss) {
$namespace = "http://search.yahoo.com/mrss/";
$images = $rss->children($namespace)->thumbnail[1]->attributes();
$image = $images['url'];
echo $image; echo "<br />";
}
代码似乎正在运行,但是在循环中的某个时刻,它只是停止了!循环永远不会遍历整个RSS Feed。我试图找出它停止的原因,但我似乎无处可去。有什么想法吗?
答案 0 :(得分:1)
我收到了错误:
Fatal error: Call to a member function attributes() on a non-object in ...
$rss->children($namespace)->thumbnail[1]->attributes()
对于某些行是空的,并且您遇到致命错误。
试试这个:
$source_xml = simplexml_load_file("http://feeds.bbci.co.uk/news/uk/rss.xml");
foreach ($source_xml->channel->item as $rss) {
$namespace = "http://search.yahoo.com/mrss/";
if(!is_object($rss->children($namespace)->thumbnail[1]))continue;
$images = $rss->children($namespace)->thumbnail[1]->attributes();
$image = $images['url'];
echo $image; echo "<br />";
}