doulan9188 2016-10-14 06: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 08: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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部