RSS syndication is very popular these days, so having at least some knowledge about RSS and how it works is a must for a PHP developer. This article explains the basics of RSS, some of its many uses, how to create an RSS feed from a database using PHP, and how to use the XML_RSS module to read an existing RSS feed and convert it to HTML.
RSS syndication is one of the most common TLAs (three-letter acronyms). RSS as an acronym has many meanings, but the current standard meaning is: Really Simple Syndication. This is the latest explanation of this extremely common and extremely useful standard.
Thinking back to the early days of the Internet, a piece of software called Pointcast pushed data to a screen saver application on a user's computer to provide various news updates. Eventually, browser developers such as Netscape and Microsoft created something like this very popular service. Netscape developed the most widely accepted variant, which was later released to the Internet development environment and eventually evolved into what is now RSS.
RSS distributes recently updated information to many recipients, much like a broadcast system. If you have a large number of users, then an RSS feed acts like a beacon to attract your users to come back for updates. Because RSS provides a much-needed way to maintain users' attention, it's no surprise that RSS is growing in popularity and being used by an increasing number of content providers.
This icon is the standard icon to indicate that a site provides RSS services for content updates. The curved lines in the icon represent radio waves, symbolizing the broadcast nature of RSS feeds.
Quite a few applications (many of them free) are capable of reading RSS feeds, and many of them allow users to aggregate feeds. The aggregation feature even allows users to further refine the quantity and nature of the content they receive. Each reader has different features designed to help manage the incredible amount of information coming from the Internet.
Mozilla Inc.’s Thunderbird and Firefox, Microsoft Corp.’s Internet Explorer 7 and upcoming versions of Office, and many others are some examples of search engines closest to you. With so many ways to get and read feeds, you should be able to find something that works for you. Unless, of course, you are a discerning software developer looking to write your own programs! This article will introduce this soon!
Your site contains content that you want to make available to the public, which is the reason you publish content on the Internet in the first place. If many users are aware of your site and content, will they come back every day to check for updates? I'm afraid not. Of the sites you visit frequently, do you visit them daily to check for updates? I'm afraid not. That's how RSS came about.
RSS can be a huge benefit to your users, especially if they find the ideas or news listed on your site valuable. Users will know exactly what you've updated or added without having to return to your site frequently, allowing users to save time and effort while never missing an update!
Generating content is not a problem if you integrate RSS feeds to support content syndication for your own site. If you pull the data from the feed and include it in your site, you can add a lot of content to your site with only a small time investment.
I personally like to use RSS to collect a feed of filtered results from different sites such as Craigslist (www.craigslist.org). A little trick I use is to use it for buying used electronics. You can set up a site search and then RSS the resulting pages. If you set up a feed that searches for cameras in a certain price range, then when someone posts a sale for a camera in your price range, you'll see it in your RSS feed! This gives you a huge advantage if you want to be the first bidder!
The RSS standard defines and contains the content of feeds. These feeds may come from any data source defined as an Internet document and are actually composed of a list of links and their descriptions.
Please view the RSS format below, which uses a sample document from NASA's "Liftoff News" feed.
<?xml version="1.0"?> <rss version="2.0"> <channel> <title>Liftoff News</title> <link>http://liftoff.msfc.nasa.gov/</link> <description>Liftoff to Space Exploration.</description> <language>en-us</language> <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate> <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate> <docs>http://blogs.law.harvard.edu/tech/rss</docs> <generator>Weblog Editor 2.0</generator> <managingEditor>editor@example.com</managingEditor> <webMaster>webmaster@example.com</webMaster> <item> <title>Star City</title> <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link> <description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's Star City.</description> <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate> <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid> </item> <item> <title>Space Exploration</title> <link>http://liftoff.msfc.nasa.gov/</link> <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a partial eclipse of the Sun on Saturday, May 31st.</description> <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate> <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid> </item> <item> <title>The Engine That Does More</title> <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link> <description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.</description> <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate> <guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid> </item> <item> <title>Astronauts' Dirty Laundry</title> <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link> <description>Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.</description> <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate> <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid> </item> </channel> </rss>
XML format document is the definition of
通道能够包含的项目数量不受限制。ITEM 元素的全部元素都是可选的,然而,至少需要一个标题或描述来验证元素。这些元素是:title、link、description、author、category、comments、enclosure、guide、pubDate和source。
本文假设您已经具有一些使用 PHP 的经验,并能够使用函数来传递变量并返回结果。PHP 拥有许多能够迅速处理应用程序内外的 XML 的函数。
首先您会希望从本地存储的数据源(一个内容管理系统、博客或任何符合 Internet 文档格式的内容)获取信息,并将其作为提要发布给您的用户。您需要获取此数据、将其格式化为 RSS 对象,然后将其提供给需要它的请求。
如果您的站点的某些部分需要额外的内容,您无需满世界地为您的站点寻找额外的内容文档,而是可以利用已经准备好的大量 RSS 提要的优势。您可使用 XML_RSS 来为您的站点获取和处理这些提要。
XML_RSS() 是一个 PEAR 软件包,用于帮助您更轻松地完成更复杂的任务 —— 解释 XML RSS 文件。PEAR 是 PHP 函数的开源库,它可免费使用并且其开发工作仍在继续。您可能已经在安装 PHP 时安装了 PEAR,但是您可能仍需为本文安装它。XML_RSS() 仅仅是一个函数,它在已知 RSS 提要的位置时,将此提要的 XML 加载到一个阵列,从而为其在您的 PHP 应用程序中的使用做好准备。此阵列的元素将具有根据被读取的 RSS 文件的元素和属性命名的键。
既然您了解了什么是 RSS 数据格式,您就可以查看您希望发布的数据,并将其转换为 RSS 格式。幸好 PHP 拥有一些功能强大的 RSS 和 XML 处理特性,这些特性可加速您的开发。和许多通用的 Web 标准一样,PHP 拥有许多功能强大的函数,可在此应用程序中使用它们。
在本文中,您将使用 "" 将数据从数据库中拉出,并将其格式化为 RSS 提要。您将对它进行设置,以便无论何时调用此 RSS 提要,它都能查找您的数据集中新增的内容,并为请求者返回新的 RSS。
提要可来自您的站点中的任何数据源,但是最终您需要确保其中包含足够的数据,以便接收 RSS 提要的人能够使用此数据。至少需要 URL 名称和描述。您的站点上发布的任何数据均可被转换为提要。
您将使用 PHP 来连接 Web 应用程序数据库、拉出更新的信息,并将其格式化为 XML RSS 文档。
假设您选择了一个数据库,您用正常方式创建一个连接,并生成一个页面以适合用户阅读的方式显示 XML 的布局。
既然您已经在您自己的代码中很好地格式化了这些数据,您就需要确保正确地发布这些数据,以便当用户将您的 URL 输入阅读器时,能够获得他们需要的 XML RSS 提要。
<?php $database = "nameofthedatabase"; $dbconnect = mysql_pconnect(localhost, dbuser, dbpassword); mysql_select_db($database, $dbconnect); $query = "select link, headline, description from `headlines` limit 15"; $result = mysql_query($query, $dbconnect); while ($line = mysql_fetch_assoc($result)) { $return[] = $line; } $now = date("D, d M Y H:i:s T"); $output = "<?xml version="1.0"?> <rss version="2.0"> <channel> <title>Our Demo RSS</title> <link>http://www.tracypeterson.com/RSS/RSS.php</link> <description>A Test RSS</description> <language>en-us</language> <pubDate>$now</pubDate> <lastBuildDate>$now</lastBuildDate> <docs>http://someurl.com</docs> <managingEditor>you@youremail.com</managingEditor> <webMaster>you@youremail.com</webMaster> "; foreach ($return as $line) { $output .= "<item><title>".htmlentities($line['headline'])."</title> <link>".htmlentities($line['link'])."</link> <description>".htmlentities(strip_tags($line['description']))."</description> </item>"; } $output .= "</channel></rss>"; echo $output; ?>
因此,让我们逐步实现此目的。首先,您设置一个到本地数据库的数据库连接对象。在此数据库中有一个具有包含标题、链接和描述字段的记录的表,您将请求把这些内容放入您的 XML 响应中。您使用 MYSQL_QUERY() 对您的表执行 SQL 查询,然后您使用 WHILE 重新格式化结果来预排将得到的对象,并将数据重新格式化为新的简单阵列。
当新的阵列准备好之后,您就可以开始使用 $output 变量构建 XML 文件,通过为每个返回的响应遍历一次 $line 阵列的方式附加新的元素。这不应该占用太多的时间,因为在您的 SQL 语句中,您将响应限制设为 15。为了将此代码片段用作起始构建块,您需要替换虚拟链接、数据库名称和登录信息来反映您自己的环境。
此脚本被执行之后,您会得到一个非常干净的 RSS 文件输出,类似于清单 3。
<?xml version="1.0"?> <rss version="0.97"> <channel> <title>Our Demo RSS</title> <link>http://www.tracypeterson.com/RSS/RSS.php</link> <description>A Test RSS</description> <language>en-us</language> <pubDate>Mon, 13 Nov 2006 22:46:06 PST</pubDate> <lastBuildDate>Mon, 13 Nov 2006 22:46:06 PST</lastBuildDate> <docs>http://someurl.com</docs> <managingEditor>you@youremail.com</managingEditor> <webMaster>you@youremail.com</webMaster> <item rdf:about="http://www.tracypeterson.com/"> <title>This is Tracy's Web Page!</title> <link>http://www.tracypeterson.com/</link> <description>This is a demonstration of how to get PHP to work for your RSS feed.</description> </item><item rdf:about="http://www.tracypeterson.com"> <title>This is Tracy's site again!</title> <link>http://www.tracypeterson.com</link> <description>Again, this is a demonstration of the power of PHP coupled with RSS.</description> </item></channel></rss>
现在任何人都可以通过输入到 RSS.php 的 URL,加载包含您的全部更新的新 RSS 文件了!
您将使用 XML_RSS() 函数将 RSS 提要导入到 PHP 脚本中,以便使其像任何阵列一样准备就绪。就像对数据库的查询一样,您将拥有一个准备好在需要时即可使用的阵列。
在本案例中,您将连接到 RSS.php 并加载一个副本,然后在无序清单中显示它。
<?php require_once "XML/RSS.php"; $rss =& new XML_RSS("http://www.tracypeterson.com/RSS/RSS.php"); $rss->parse(); echo "<h1>Headlines from <a href="http://www.tracypeterson.com/RSS/RSS.php">Tracy Peterson's Site</a></h1>n"; echo "<ul>n"; foreach ($rss->getItems() as $item) { echo "<li><a href="" . $item['link'] . "">" . $item['title'] . "</a></li>n"; } echo "</ul>n"; ?>
此示例直接来自 PEAR 手册,我使用它是因为它非常简练。让我们逐行查看它来看看它是否仅使用了 XML_RSS() 类、构造器和 parse()提供的几个方法。解析操作仅将输出作为我之前提到的阵列呈现。
首先,您使用 require_once() 函数来从 PEAR 安装中加载 RSS.php 文件。如果正确设置了 PEAR 并安装了 XML_RSS ,那么它将正确地找到此引用文件,然后 XML_RSS 对象就准备好供您使用了。接下来,您创建一个名为 $rss 的新对象,此对象是将提要的 URL 传递到您的 XML_RSS 构造器的结果。
您仅使用 parse() 方法来返回 RSS 提要中的值。第一个 echo 行开始设置用于使 RSS 提要能够被用户阅读的基本 HTML。在本案例中,您声明此无序清单是来自我的站点的标题的清单!
foreach() 语句通过将 getItems() 方法用作新的阵列 $items,从解析的提要中获取每个项目元素。每个阵列元素根据包含它们的实际 XML 标签命名。在本案例中,您仅使用了链接和标题,一会您将添加对它们的描述。每处理一次 foreach 循环,就会移动到下一个元素,直到整个 RSS 提要以此方式全部显示出来。
现在,将描述添加到每个显示的结果中。
下面代码行添加到 foreach() 循环中。
foreach ($rss->getItems() as $item) { echo "<li>" . $item['title'] . "</li><br>"; echo $item['description'] . "<br><br>n"; }
您只需向无序清单中添加一个换行符和描述行。
总的说来,Internet 才刚刚开始意识到它拥有的难以置信的潜力。通过 RSS 提供的提高了的可访问性,您现在能够简化使您的用户保持同步的过程。您能够及时将更新通知到用户,而无需用户花费时间来检查您是否添加了新信息。
您刚刚探索了 RSS 格式的标准,以及如何创建提要、接收提要并将其转换为可用的 HTML。现在您已经准备好在更大型的应用程序中使用这些技能了。