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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?