本文介绍了使用getElementsByTagName用php检索rss图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有这段代码,但是它在getAttributes
行上给了我一个错误,我无法毕生弄清楚原因.
I have this code here but it is giving me an error on the getAttributes
line and I cant for the life of me figure out why.
这是我收到的错误消息:
This is the error message I get:
<?php
$rss = new DOMDocument();
$rss->load('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/uk/rss.xml');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'image' => $node->getElementsByTagName('thumbnail')->item(0)->getAttribute('url')
);
array_push($feed, $item);
}
?>
推荐答案
也许不是对象,请尝试查看元素是否存在:
Maybe that is not an object, try to see if element exists:
'image' => $node->getElementsByTagName('thumbnail')->item(0) ? $node->getElementsByTagName('thumbnail')->item(0)->getAttribute('url') : ''
这篇关于使用getElementsByTagName用php检索rss图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!