使用getElementsByTagName用php检索rss图像 [英] Retrieving rss images with php using getElementsByTagName

查看:100
本文介绍了使用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:

致命错误:在第58行上的/Applications/MAMP/htdocs/blogDepot/application/pages/myBlogs/index.php中的非对象上调用成员函数getAttribute()

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);
}
?>

推荐答案

也许不是对象,请尝试查看元素是否存在:

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图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆