duanliangman5398 2016-05-16 17:07
浏览 31
已采纳

PHP读取RSS提要在节点中的第三个链接上获取错误

I am reading a RSS feed and each node has 3 links:

<link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2202110476673931679/6339893542751280730/comments/default/1280042367141045524'/>

<link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2202110476673931679/6339893542751280730/comments/default/1280042367141045524'/>

<link rel='alternate' type='text/html' href='http://misterika.blogspot.com/2016/04/blog-post_11.html?showComment=1460801110852#c1280042367141045524' title=''/>

I read the "href" attribute with this:

'link' => $node->getElementsByTagName('link')->item(0)->getAttribute('href')

There is no problem when I use item(0) for the first link, there is no problem when I use item(1) for the second link but when I use item(2) for the third link I get this error:

Fatal error: Call to a member function getAttribute() on a non-object

Any idea how can I solve it?

Here is my full code:

<?php
$rss = new DOMDocument();

$rss->load('http://misterika.blogspot.com/feeds/comments/default');

$feed = array();
foreach ($rss->getElementsByTagName('entry') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('name')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('content')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(2)->getAttribute('href'),
        'date' => $node->getElementsByTagName('published')->item(0)->nodeValue,
        );
    array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<small><em>Posted on '.$date.'</em></small></p>';
    echo '<p>'.$link.'</p>';
    echo '<p>'.$description.'</p>';
}

?>

</div>
  • 写回答

2条回答 默认 最新

  • doujianqin5172 2016-05-16 17:23
    关注

    It's Working when I tested with the below sample snippet.

    <?php
    
    $xml = "<root><entry><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2202110476673931679/6339893542751280730/comments/default/1280042367141045524'/>
    
    <link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2202110476673931679/6339893542751280730/comments/default/1280042367141045524'/>
    
    <link rel='alternate' type='text/html' href='http://misterika.blogspot.com/2016/04/blog-post_11.html?showComment=1460801110852#c1280042367141045524' title=''/></entry>
    
    <entry><link rel='edit' type='application/atom+xml' href='http://google.com/'/>
    
    <link rel='self' type='application/atom+xml' href='http://jenson.in/'/></entry></root>";
    
    $node = new DOMDocument;
    $node->loadXML($xml);
    
    foreach($node->getElementsByTagName("entry") as $entry)
    {
        $link = $entry->getElementsByTagName("link");
    
        echo $node->getElementsByTagName('link')->item(0)->getAttribute('href')."<br/>";
        echo $node->getElementsByTagName('link')->item(1)->getAttribute('href')."<br/>";
       //Below code checks if third link exists or not.
       echo ($link->length > 2)?$node->getElementsByTagName('link')->item(2)->getAttribute('href'):"No alternate link!"."<br/>";
    }   
    ?>  
    

    See Demo

    UPDATE:

    In your Feed XML, There is no 3rd link after http://misterika.blogspot.com/2016/03/blog-post_20.html?showComment=1462627509971#c2966841279736454385 Only 2 links available in that entry node. That's why you're getting error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,