使用PHP解析RSS / Atom提要的最佳方法

时间:2008-10-30 15:46:12

标签: php parsing rss atom-feed

我目前正在使用Magpie RSS但有时在RSS或Atom Feed形成不良时会出现问题。有没有其他选项可以用PHP解析RSS和Atom提要?

10 个答案:

答案 0 :(得分:162)

我一直使用the SimpleXML functions built in to PHP来解析XML文档。它是为数不多的通用解析器之一,它具有直观的结构,这使得为特定类似RSS提要的东西构建有意义的类非常容易。此外,它会检测XML警告和错误,并且在找到任何内容后,您可以通过HTML Tidy(如ceejayoz提到的)之类的方式运行源代码来清理它并再次尝试。

使用SimpleXML考虑这个非常粗略,简单的类:

class BlogPost
{
    var $date;
    var $ts;
    var $link;

    var $title;
    var $text;
}

class BlogFeed
{
    var $posts = array();

    function __construct($file_or_url)
    {
        $file_or_url = $this->resolveFile($file_or_url);
        if (!($x = simplexml_load_file($file_or_url)))
            return;

        foreach ($x->channel->item as $item)
        {
            $post = new BlogPost();
            $post->date  = (string) $item->pubDate;
            $post->ts    = strtotime($item->pubDate);
            $post->link  = (string) $item->link;
            $post->title = (string) $item->title;
            $post->text  = (string) $item->description;

            // Create summary as a shortened body and remove images, 
            // extraneous line breaks, etc.
            $post->summary = $this->summarizeText($post->text);

            $this->posts[] = $post;
        }
    }

    private function resolveFile($file_or_url) {
        if (!preg_match('|^https?:|', $file_or_url))
            $feed_uri = $_SERVER['DOCUMENT_ROOT'] .'/shared/xml/'. $file_or_url;
        else
            $feed_uri = $file_or_url;

        return $feed_uri;
    }

    private function summarizeText($summary) {
        $summary = strip_tags($summary);

        // Truncate summary line to 100 characters
        $max_len = 100;
        if (strlen($summary) > $max_len)
            $summary = substr($summary, 0, $max_len) . '...';

        return $summary;
    }
}

答案 1 :(得分:36)

有4行,我将rss导入数组。

$feed = implode(file('http://yourdomains.com/feed.rss'));
$xml = simplexml_load_string($feed);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

更复杂的解决方案

$feed = new DOMDocument();
 $feed->load('file.rss');
 $json = array();
 $json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
 $json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
 $json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
 $items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');

 $json['item'] = array();
 $i = 0;

 foreach($items as $key => $item) {
 $title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
 $description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
 $pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
 $guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;

 $json['item'][$key]['title'] = $title;
 $json['item'][$key]['description'] = $description;
 $json['item'][$key]['pubdate'] = $pubDate;
 $json['item'][$key]['guid'] = $guid; 
 }

echo json_encode($json);

答案 2 :(得分:29)

您的其他选择包括:

答案 3 :(得分:17)

我想介绍简单的脚本来解析RSS:

$i = 0; // counter
$url = "http://www.banki.ru/xml/news.rss"; // url to parse
$rss = simplexml_load_file($url); // XML parser

// RSS items loop

print '<h2><img style="vertical-align: middle;" src="'.$rss->channel->image->url.'" /> '.$rss->channel->title.'</h2>'; // channel title + img with src

foreach($rss->channel->item as $item) {
if ($i < 10) { // parse only 10 items
    print '<a href="'.$item->link.'">'.$item->title.'</a><br />';
}

$i++;
}

答案 4 :(得分:12)

如果feed不是格式良好的XML,那么你应该拒绝它,没有例外。您有权致电饲料创建者a bozo

否则,你正在为弄乱HTML结束而铺平道路。

答案 5 :(得分:6)

HTML Tidy库能够修复一些格式错误的XML文件。在将它们传递给解析器之前运行它们可能有所帮助。

答案 6 :(得分:2)

我使用SimplePie来解析Google阅读器Feed,它运行良好且具有相当不错的功能集。

当然,我还没有使用非格式良好的RSS / Atom提要测试它,所以我不知道它是如何应对的,我假设Google的标准相当合适! :)

答案 7 :(得分:1)

我个人使用BNC Advanced Feed Parser-我喜欢非常容易使用的模板系统

答案 8 :(得分:1)

PHP RSS阅读器 - http://www.scriptol.com/rss/rss-reader.php - 是一个完整但简单的解析器,被数千名用户使用......

答案 9 :(得分:-1)

另一个很棒的免费解析器 - http://bncscripts.com/free-php-rss-parser/ 它非常轻(仅3kb)并且使用简单!

相关问题