我目前正在使用Magpie RSS,但是当RSS或Atom提要不是很好时,它有时会失败.是否还有其他选项可以使用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 = $_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);
您的其他选择包括:
了SimplePie
最后的RSS
PHP Universal Feed Parser
我想介绍简单的脚本来解析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 ''.$rss->channel->title.'
'; // channel title + img with src foreach($rss->channel->item as $item) { if ($i < 10) { // parse only 10 items print ''.$item->title.'
'; } $i++; }
如果feed不是格式良好的XML,那么你应该拒绝它,没有例外.您有权将饲料创建者称为bozo.
否则,你正在铺平道路,弄乱HTML最终.
HTML Tidy库能够修复一些格式错误的XML文件.在将它们传递给解析器之前运行您的feed可能会有所帮助.