doulan9188 2016-10-14 14:26
浏览 25
已采纳

如何使用PHP从第三方网站解析格式错误的RSS源?

I'm trying to parse RSS feeds from some medias. My script works for most of them. The problem is that I need to agregate all of them, eventhough they are malformed.

I don't manage to get the description of these two feeds. How could I proceed anyway ?

Here is my script :

<?php
function RSS_items ($url) {
    $i = 0;
    $doc = new DOMDocument();
    $doc->load($url);
    $channels = $doc->getElementsByTagName('channel');
    foreach($channels as $channel) {
        $items = $channel->getElementsByTagName('item');
        foreach($items as $item) {
            $i++;
            $y[$i]['title'] = $item->getElementsByTagName('title')->item(0)->firstChild->textContent;
            $y[$i]['link'] = $item->getElementsByTagName('link')->item(0)->firstChild->textContent;
            $y[$i]['updated'] = $item->getElementsByTagName('pubDate')->item(0)->firstChild->textContent;
            $y[$i]['description'] = $item->getElementsByTagName('description')->item(0)->firstChild->textContent;
        }
    }
    echo '<pre>';
    print_r ($y);
    echo '</pre>';
}
// the two malformed feeds
RSS_items ('http://www.lefigaro.fr/rss/figaro_actualites-a-la-une.xml');
RSS_items ('https://francais.rt.com/rss');
?>
  • 写回答

1条回答 默认 最新

  • dongpanshi2839 2016-10-14 16:13
    关注

    Problem of your code is in useing firstChild property that select first child of element. But in target XML, description tag hasn't any childs that you want to select first of them. Remove it from code. The result should be like this

    $item->getElementsByTagName('description')->item(0)->textContent;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)