Retrieving rss images with php using getElementsByTagName [closed]

痴心易碎 提交于 2019-12-24 00:59:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!