我目前正在使用 喜鹊RSS 但当 RSS 或 Atom feed 格式不正确时,它有时会失败。是否还有其他选项可以使用 PHP 解析 RSS 和 Atom 提要?

有帮助吗?

解决方案

其他提示

我一直使用 PHP内置的SimpleXML函数来解析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 = 

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

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

<*>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; } }

有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);

我想介绍简单的脚本来解析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++;
}

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

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

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

我使用 SimplePie 来解析Google阅读器Feed,它运行良好且功能设置不错。

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

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

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top