12.12阅读RSS和Atom Feeds
12.12.1问题
You want to retrieve RSS and Atom feeds and look at the items. This allows you to incorporate newsfeeds from
multiple web sites into your application.
你想要找回RSS和Atom feeds和考虑在这个items. 它允许你合并newsfeeds从多种web sites到你的应用程序
12.12.2解答
Use the MagpieRSS parser. Here’s an example that reads the RSS feed for the php.announce mailing list:
使用这个MagpieRss 剖析器。这里是一个例子阅读Rss feed到这个php.annouce邮件发送清单
?php
require 'rss_fetch.inc';
$feed = 'http://news.php.net/group.php?group=php.announce&format=rss';
$rss = fetch_rss( $feed );
print "<ul>\n";
foreach ($rss->items as $item) {
print '<li><a href="' . $item['link'] . '">' . $item['title'] . "</a></li>\n";
}
print "</ul>\n";
12.12.3讨论
RSS (RDF Site Summary) is an easy-to-use headline or article syndication format written in XML.[] Many news
web sites, such as the New York Times and the Washington Post, provide RSS feeds that update whenever new
stories are published. Weblogs have also embraced RSS and having an RSS feed for your blog is a standard
feature. The PHP web site also publishes RSS feeds for most PHP mailing lists.
RSS(RDF主要摘要)是一个很容易使用和表达syndication 格式在XML.[]很多的新的web sites 。例如纽约时间和华盛顿时间
。规定RSS feeds 修正无论新的存储内容是pubished,博客同样经常使用RSS和蚩尤一个RSS feed在你的bolg是一个标准的特
质。这个PHP web站点同样订阅RSS feeds在更多的PHP mailing lists
[] RDF stands for Resource Definition Framework. RSS also stands for Rich Site Summary.
Atom is a similar XML syndication format. It extends many of the concepts in RSS, including a way to read and
write Atom data. It also attempts to provide a more well-defined syntax for syndication than RSS, as the RSS
specification doesn’t always clearly enumerate exactly what is or isn’t permissible in a feed.
Atom 是一个相似的XML syndication格式。它延展更多观念到RSS .包括一个办法去阅读和写Atom 数据。它同样尝试规定一
个更多的well-defined语法在syndication比RSS. 当作RSS规范不能明显的列举正确的
Using MagpieRSS, retrieving and parsing RSS and Atom feeds are simple:
使用MagpieRss。找回分解RSS和Atom feeds是简单的
?php
$feed = 'http://news.php.net/group.php?group=php.announce&format=rss';
$rss = fetch_rss($feed);
This example reads in the RSS feed for the php.announce mailing list. The feed is then parsed by fetch_rss( )
and stored internally within $rss.
这个例子阅读在这个RSS feed在这个php.announce mailing list 。这个feed是分解fetch_rss()和存储到$rss内
While this feed is RSS 0.93, there’s no need to specify this to MagpieRSS. Its fetch_rss( ) function detects
the syndication format, including Atom, and formats the document accordingly.
使用这个feed是RSS 0.93 ,它不需要指定这些到MagpieRSS .它的fetch_rss()函数发现这个syndication格式。包括Atom 和
这个格式
Each RSS item is then retrieved as an associative array using the items property:
每个RSS消息重新找回当作一个联合数组使用items 特质
?php
print "<ul>\n";
foreach ($rss->items as $item) {
print '<li><a href="' . $item['link'] . '">' . $item['title'] . "</a></li>\n";
}
print "</ul>\n";
This foreach loop creates an unordered list of items with the item title linking back to the URL associated
with the complete article, as shown in Figure 12-1. Besides the required title and link fields, an item can
have an optional description field that contains a brief write-up about the item.
这个foreach循环创造一个无序目录消息在这个item字幕背景到联合URL和全部文章。例如在图像12-1.除此之外必须的标题和
链接域。一个item可以有一个可选择的描写 field包含一个主要write-up这个item
Each channel also has an entry with information about the feed, as shown in Figure 12-2. To retrieve that
data, call access the channel attribute:
每个channel同样有一个入口和这个feed的信息。例如在图像12-2,去重新找回那个数据调用访问这个频道的特质
?php
$feed = 'http://news.php.net/group.php?group=php.announce&format=rss';
$rss = fetch_rss($feed);
print "<ul>\n";
foreach ($rss->channel as $key => $value) {
print "<li>$key: $value</li>\n";
}
print "</ul>\n";