我目前正在使用Magpie RSS,但是当 RSS 或 Atom 提要格式不正确时,它有时会崩溃。是否有任何其他选项可用于使用 PHP 解析 RSS 和 Atom 提要?
10 回答
我一直使用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);
您的其他选择包括:
我想介绍一个简单的脚本来解析 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++;
}
如果提要不是格式良好的 XML,您应该拒绝它,没有例外。您有权称提要创建者为 bozo。
否则,您正在为最终导致 HTML 混乱铺平道路。
HTML Tidy 库能够修复一些格式错误的 XML 文件。在将它们传递给解析器之前运行你的提要可能会有所帮助。
我使用SimplePie来解析 Google Reader 提要,它运行良好,并且具有不错的功能集。
当然,我还没有使用非格式良好的 RSS / Atom 提要对其进行测试,所以我不知道它是如何应对这些的,我假设 Google 是相当符合标准的!:)
我个人使用 BNC Advanced Feed Parser - 我喜欢非常易于使用的模板系统
PHP RSS 阅读器 - http://www.scriptol.com/rss/rss-reader.php - 是一个完整而简单的解析器,成千上万的用户都在使用...
另一个很棒的免费解析器 - http://bncscripts.com/free-php-rss-parser/ 它非常轻巧(只有 3kb)并且使用简单!