我这里有这个代码,但它在getAttributes
行上给了我一个错误,我不知道为什么我会找出原因。
这是我收到的错误消息:
致命错误:在第58行的/Applications/MAMP/htdocs/blogDepot/application/pages/myBlogs/index.php中的非对象上调用成员函数getAttribute()
<?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);
}
?>
答案 0 :(得分:2)
也许这不是一个对象,试着看看元素是否存在:
'image' => $node->getElementsByTagName('thumbnail')->item(0) ? $node->getElementsByTagName('thumbnail')->item(0)->getAttribute('url') : ''