问题
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:
Fatal error: Call to a member function getAttribute() on a non-object in /Applications/MAMP/htdocs/blogDepot/application/pages/myBlogs/index.php on line 58
<?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);
}
?>
回答1:
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') : ''
来源:https://stackoverflow.com/questions/12769712/retrieving-rss-images-with-php-using-getelementsbytagname