< Day Day Up > 

Creating RSS Feeds

As you're probably aware, the version of Safari included in Tiger now supports RSS "channels." RSS (Rich Site Summary, RDF Site Summary, or Really Simple Syndication depending on who you ask and the time of day) provides a simple means of syndicating your website's content. If your site offers an RSS channel, any other website can display a summary of the content you have available. This is often employed on blogs and news sites but could be extended to just about any type of information that you might want to share: recipes, file uploads, and so on. The benefit of RSS is that it promotes content sharing rather than the content "borrowing" (stealing) that is prevalent. Sites can share information and offer customized RSS feeds to suite their users needs.

Although RSS is still in the early implementation stages for many sites, it has gained considerable momentum through acceptance by online news organizations and, sadly, advertisers. Integration into Safari and the popular cross-platform Firefox browser will further push its adoption by site owners around the world. If you aren't using RSS now, you should be. The good news, implementing RSS on a website is absolutely trivial, even if you just maintain a few static web pages.

Understanding the Components of an RSS Feed

If you're like many folks, you've heard the world extolling the benefits of XML for several years, but probably haven't seen many practical ways for you to use it personally. RSS is an application of XML that is easy to understand and demonstrates the power of the cross-platform markup language. To begin, let's take a look at Listing 24.11, an RSS feed published by NASA during the Ivan hurricane near the end of 2004.

Listing 24.11. A Sample RSS File
  1: <?xml version="1.0" encoding="UTF-8"?>  2: <rss version="2.0">  3:        <channel>  4:               <title>NASA Coverage of Hurricane Ivan</title>  5:               <link></link>  6:               <description>How Hurricane Ivan was studied by NASA and                                how it affected NASA facilities.</description>  7:               <language>en-us</language>  8:               <docs></docs>  9:               <managingEditor></managingEditor> 10:               <webMaster></webMaster> 11:               <item> 12:                      <title>Hurricane Ivan's "CAT" Scan</title> 13:                      <link>/vision/earth/lookingatearth/IVAN_TRMM_9.15.html                          </link> 14:                      <description>NASA looks inside the storm.</description> 15:                      <pubDate>09.15.04</pubDate> 16:               </item> 17:               <item> 18:                      <title>Ivan Inside Out</title> 19:                      <link>                                /Ivan_inside_out.html</link> 20:                      <description>Satellites look inside Ivan to find clues                  about its intensity as the storm approaches land.</description> 21:                      <pubDate>09.20.04</pubDate> 22:               </item> 23:               <item> 24:                      <title>Ivan's 'CAT' Scan</title> 25:                      <link>                                IVAN_TRMM_9.15.html</link> 26:                      <description>NASA looks at Ivan's engine.</description> 27:                      <pubDate>09.20.04</pubDate> 28:               </item> 29:               <item> 30:                      <title>NASA Prepares for Hurricane Ivan</title> 31:                      <link>                                HQ_04299_SSCprep_.html</link> 32:                      <description>Weather forecasts indicate some NASA     centers and facilities could feel Ivan's terrible wrath.</description> 33:                      <pubDate>09.14.04</pubDate> 34:               </item> 35:               </channel> 36: </rss> 

The start of all valid RSS files must declare the version of XML and RSS standards employed, as seen in lines 1 2. For virtually all feeds you'd be creating at this time, the proper values will be "1.0" and "2.0", respectively. Next, a <channel> (line 3) tag marks the start of the information related to the content you are syndicating. A channel requires several mandatory metadata tags to be implemented in order to be considered valid. These tags are

<title></title> The official name for the channel. This should be a clear, concise, and descriptive string (line 4).

<link></link> A full URL to the website that the RSS channel corresponds to (line 5).

<description></description> A brief description of what users can expect to find on a channel (line 6).


There is no hard-and-fast rule for how large attributes should be (such as the description, title, and so forth), but the implementation of RSS channels within many reader applications typically makes some assumptions when laying out the displays. For example, titles are short, descriptions are one or two sentences, and so on. Use common sense when creating your channels so that they display properly in RSS readers.

In addition to mandatory tags, there are several additional tags available to describe the channel. In the sample channel, for example, NASA has implemented the <managingEditor>, <webMaster>, <docs>, and <language> tags (lines 7 10.) A list of the most useful optional tags is provided here:

<managingEditor> The editor of the content provided by the channel.

<webMaster> The individual in charge of the server providing the channel content.

<docs> A URL that points to the RSS specification implemented in the channel, most likely "".

<language> The language used for the channel content. A list of acceptable languages can be found at$15.

<rating> A PICS rating for the channel to help control access to the content. Details on PICS can be found at

<copyright> A copyright notice for the channel.

<ttl> The time a subscriber of an RSS channel can use the channel content without refreshing it.

<lastBuildDate> The time of the last update to the channel.

After the basic channel properties are defined, the actual content must be provided. In the NASA feed, this is accomplished in lines 11 34, which defines each of four different stories within the <item></item> tags. Each item can be either a description and link to content or the entire content itself. The most common tags that can be used in each item definition are

<title> The title of the article, story, or other content piece.

<link> A URL that points to the specific piece of content.

<description> In many cases, the description is treated as a document abstract. It should describe the content that is linked to. If desired, the description can be the content, in which case the title and link are not required.

<author> An email address for an article's author.

<pubDate> The publication date for the article.

After the articles have been defined, the RSS channel is closed with a </channel> and </rss> close tag. Based on the patterns set up in this sample, you should very easily be able to create your own RSS documents to include in your website.

RSS feeds are generally used to define content that changes frequently; manually keeping a document up-to-date can become a bit of a chore. To streamline the process, you can use an application such as Glass Onion Software's FeedMe ( shown in Figure 24.9, to create and manage RSS feeds within a GUI environment.

Figure 24.9. Onion Software's FeedMe can be used to manage RSS feeds within the Tiger GUI.

Even with tools to help build RSS feeds, if your site publishes more than an article a day, you're likely to find yourself tiring of the process quickly. If your server provides access to PHP or another scripting language, you should definitely consider dynamically building the feed on the fly.


When creating an RSS feed, especially by hand, be sure to validate the code before putting it online. The site provides free validation services and RSS banners that can be used to promote your standards-compliant RSS-enabled site.

Using PHP and FeedCreator to Build an RSS Feed

PHP can be used to create an RSS feed by dynamically generating the appropriate RSS tags instead of HTML. To make things even easier, the work has already been done for you. Free PHP libraries have been written that enable web developers such as you to build RSS channels without having to remember the document syntax and parse existing channels for inclusion in other websites. The library that we'll discuss here is FeedCreator, which you can download from

FeedCreator is a very simple to use PHP class that builds and saves valid RSS files, and, best of all, it's free. After downloading the source distribution, unzip it and review the contents. You should see two files: a license file, which you should take the time to read, and a PHP file called feedcreator.class.php. You will need to place this file in a location where it can be included by the PHP script you want to build the feed. You'll also need to make one change to the file before continuing: changing the default time zone.

To set the time zone in feedcreator.class.php, open the file in an editor and search for the line:


Edit the number at the end of the line to reflect your time zone's offset from GMT, and then save your change. The file is now ready for use. One more change and you're ready to go. To keep from overwhelming your server by querying a data source repeated to build the RSS channel, FeedCreator will create an XML file with the contents of the feed and will attempt to use this cached version of the feed if the file is less than 60 minutes old. To create this file, FeedCreator requires a directory that your web server can write to.

Create a directory in your web space (such as ~/Sites/rsscache) and set "www" as either the owner or the group of that directory (that is, sudo chown www <folder containing rss cache file>).

Now you're ready to generate your RSS feed. To get you started, I've created a sample feed for a single story on the Mac OS X Tiger Unleashed support site. Review the code in Listing 24.12, and then we'll step through the lines to get a feel for what it does.

Listing 24.12. FeedCreator Can Generate RSS 2.0 Files
  1: <?php  2: include("feedcreator.class.php");  3:  4: $rss = new UniversalFeedCreator();  5: $rss->useCached();  6: $rss->title = "Tiger Unleashed News";  7: $rss->description = "Updates and Errata for the Mac OS X Unleashed Series";  8: $rss->link = "";  9: 10: $item = new FeedItem(); 11: $item->title = "Mac OS X Tiger Unleashed Released"; 12: $item->link = ""; 13: $item->description = "The Tiger Edition of Mac OS X Unleashed has hit the                           stores.  Read about it now."; 14: $item->date = "Fri, 01 Apr 2005 12:59:23 -0500"; 15: $item->author = "John Ray"; 16: $rss->addItem($item); 17: 18: echo $rss->saveFeed("RSS2.0", "rsscache/myfeed.xml"); 19: 20: ?> 

Just looking at the listing you can probably very quickly identify the components that make up an RSS channel. The FeedCreator class is included in line 2; this gives us the features and functions needed to build the channel.

To start the channel, a new RSS object ($rss) is created, as demonstrated in line 4. Line 5 attempts to use a cached version of the feed if the cache is less than an hour old.

Lines 6 8 set up the three required components of describing an RSS channel: the title, description, and link. After the channel itself has been created, items are added.

Line 10 creates a new item ($item), whereas lines 11 15 set the title, link, description, date, and author, respectively. Finally, line 16 adds the item to the RSS channel ($rss).

On a real-world system, you would likely want to query a database or other data source and loop through lines 10 16 repeatedly for each story or article; you may use the same $item object for each iteration of the loop.

Finally, the RSS XML file is output and saved to the named file (myfeed.xml) in line 18. Note that the file is saved in the rsscache directory which must be writeable by the www user account.


Line 18 also sets the version of RSS to use. RSS 2.0 is the latest and cleanest spec, and is what we've shown in this chapter. You can use older specs by specifying RSS0.91 or RSS1.0. There are additional syndication features and formats supported and described within the comments at the top of the feedcreator.class.php.

The output of this simple script should very closely resemble the following:

 <?xml version="1.0" encoding="ISO-8859-1"?> <!-- generator="FeedCreator 1.7.2" --> <rss version="2.0">     <channel>         <title>Tiger Unleashed News</title>         <description>Updates and Errata for the Mac OS X Unleashed Series         </description>         <link></link>         <lastBuildDate>Sat, 04 Dec 2004 13:00:27 -0500</lastBuildDate>         <generator>FeedCreator 1.7.2</generator>         <item>             <title>Mac OS X Tiger Unleashed Released</title>             <link></link>             <description>The Tiger Edition of Mac OS X Unleashed has hit the                          stores.  Read about it now.</description>             <author>John Ray</author>             <pubDate>Fri, 01 Apr 2005 12:59:23 -0500</pubDate>         </item>     </channel> </rss> 

The benefits of automatically generating RSS feeds should be obvious: News of your latest publications can reach your content consumers without them needing to visit your website and without any additional work on your part. All done? Not just yet; there is still one more thing you need to do to publicize your feed.

Promoting Your RSS Feed in Safari RSS

As you've probably noticed, when you're browsing a web page in Safari and the page offers an RSS channel, a small blue RSS button appears in the URL, as shown in Figure 24.10. This enables you to quickly load and subscribe to the given feed. The page you were likely browsing, however, probably was an HTML page, so how did Safari know that an RSS feed was available?

Figure 24.10. Safari identifies available RSS feeds.

The answer is a simple alternative content link in the <head></head> portion of the content page. For example:

 <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">        <link rel="alternate" title="Mac OS X Tiger Unleashed RSS"         href="" type="application/rss+xml">        <title>Mac OS X Tiger Unleashed</title> </head> 

Here, the Mac OS X Tiger Unleashed RSS feed is promoted by adding the following tag:

 <link rel="alternate" title="Mac OS X Tiger Unleashed RSS" href="" type="application/rss+xml"> 

You can do the same for the pages of your site that offer RSS channels by inserting the appropriately formatted HTML tags into your documents.

Parsing RSS Feeds

Because you have the tools to create RSS feeds, it might also be useful to parse existing RSS feeds to include external content on your own website (called aggregation.) As with creating feeds, this is accomplished very easily by taking advantage existing PHP classes. MagpieRSS is one of the most popular, powerful, and easy-to-use PHP parsers. You can download the source code from

The MagpieRSS distribution will unarchive to a directory containing several files. You should place these in a location where they can be included from the PHP page where you want a feed displayed. Within the page itself, you will need to add a code block that displays the feed. For example, consider Listing 24.13.

Listing 24.13. MagpieRSS Can Easily Parse and Render RSS Feeds
  1: <?php  2:  3: require('');  4: $rss = fetch_rss("");  5:  6: foreach ($rss->items as $item) {  7:      $link = $item['link'];  8:      $title = $item['title'];  9:      $description = $item['description']; 10:      print "<b>$title</b><br/>"; 11:      print "$description... "; 12:      print "<a href=\"$link\">[Read More]</a><br/>"; 13:      print "<br/>"; 14: } 15: ?> 

This small piece of code generates page content similar to what you see in Figure 24.11.

Figure 24.11. The MagpieRSS libraries can easily read and parse external RSS feeds for your website.

Of course you can customize the display to your liking. The code displayed here works as follows:

Line 3 includes the necessary MagpieRSS library ( Next, line 4 creates an RSS object ($rss) based on the URL provided (if you'd like to test this on your system, the URL in this example is real).

The display is output in lines 6 14, which loops through each of the items in the RSS object. On each iteration of the loop, an associative array $item is assigned the values from the RSS channel. Each item attribute can be retrieved by indexing into the $item array using the attribute name $item['title'] for the title, $item['author'] for the author, and so on.

Because the output is just HTML, you can customize the display in any manner you'd like. There is additional documentation and sample scripts available in the MagpieRSS Cookbook at

     < Day Day Up > 

    Mac OS X Tiger Unleashed
    Mac OS X Tiger Unleashed
    ISBN: 0672327465
    EAN: 2147483647
    Year: 2005
    Pages: 251 © 2008-2017.
    If you may any questions please contact us: