Note: Assuming your browser supports it, this list will not be printed out. Hide navigation

Writing An RSS Feed

Friday 9th September 2005

Introduction

Before we begin, if you are not very sure about what an RSS feed really is, you should read up on RSS - you need to know about RSS from a user's perspective before you start writing it!

As you are probably aware, using feeds is increasingly popular on the internet. Almost any site could have a use for a feed, from the latest news to new articles. When we look at the benefits having a feed has, using a feed becomes very attractive. This article will explain how to create an RSS feed, and put it on your website.

History

First of all, we need to decide on the type of feed we are going to use. We have a number of choices here. Over the years, there have been a number of RSS versions. If you don't care about the history, and just want to learn how to create a feed, feel free to skip ahead to the next section. Here they are in a nice list:

As you can see, that is a lot of versions, not to mention the Atom feeds. There are two main lines of RSS - RSS 0.9x with RSS 2.0, and RSS 1.0. The various versions of RSS 0.9x and RSS 2.0 are largely, although not completely, compatible.

Netscape created the first RSS version, 0.90, followed by 0.91. Userland then took over, and created its own version of 0.91. It continued with RSS 0.92, 0.93 and 0.94, although 0.93 and 0.94 were not intended for actual use. This was followed by RSS 2.0. 2.0 was updated, and became 2.01,followed by another update, again called RSS 2.01. Although all three versions were slightly different from each other, there is no way for feed readers to tell the difference between any of them - they all identify themselves simply as RSS 2.0.

The RSS1.0 line is somewhat simpler. It is separate and incompatible with the other versions of RSS since it follows the RDF standards. This makes it somewhat more complicated to write.

Choosing The Feed Version

So now we come to that important decision, which feed do we actually use? First of all, I'm going to exclude Atom. Although it is relatively well known, RSS is still more likely to be supported than Atom. If Atom were to be better supported, I would probably go for Atom. Unlike Atom, RSS is not controlled by a standards body or consortium, meaning the specifications can be changed at a whim.

Next to go is RSS 1.0. Although it is useful, and being RDF based can be part of the semantic web, it is more complicated than the other versions. As such, we are left with RSS 0.9x and RSS 2.0. It makes most sense to go for RSS 2.0, since this is the latest version.

Before we begin learning to write an RSS 2.0 feed, lets take a look at a basic example:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>Title of the Feed</title>
<link>http://www.example.com</link>
<description>This is an example of an RSS 2.0 feed.</description>
<language>en-gb</language>
<copyright>The copyright notice</copyright>

<item>
<title>An Item in The Feed</title>
<link>http://www.example.com/news.html</link>
<description>This is an example of an item in an RSS 2.0 Feed.</description>
<pubDate>Thu, 03 Sep 2005 17:00:00 GMT</pubDate>
</item>

</channel>
</rss>

Writing the Basic Feed

A feed is generally made up of a channel, and the items inside the channel. Before that, we need to define the file as an RSS 2.0 file. We do that using these two lines of code right at the beginning of the document:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">

You should also save the file with the extension .xml. If you are using PHP to dynamically generate the feed, you need to make sure the feed is sent as XML. To do this, add this line right at the beginning:

<?php header('Content-type: text/xml');?>

Next, we open the channel, using the channel tag:

<channel>

We need to tell the reader of the feed some information about the feed. We need a title, a link, and a description. The title and description are self explanatory, while the link should point to the main page of the topic of the feed. For example, if this was a feed about beans on example.com, this section might look similar to this:

<title>Beans on example.com</title>
<link>http://www.example.com/beans/</link>
<description>The latest news on beans.</description>

There are also several optional parts. There are two I highly recommend you add - language and copyright. These define the language and provide a copyright statement respectively. The list of allowable languages can be found in the RSS 2.0 specification. Note that, should you want to use the copyright symbol, you must write &#169;. The result might be this:

<language>en-gb</language>
<copyright>© 2005 example.com</copyright>

Other parts you might want to add are the e-mail addresses of the 'managing editor' and the 'webmaster', using <managingEditor> and <webMaster> respectively, as well as the last time the feed was updated, using <lastBuildDate>. This must be in the correct format: Sat, 07 Sep 2002 00:00:01 GMT. That is, the day in three letters; comma; the day of the month with a leading zero; the year in four digits; the time, including hours, minutes, seconds, all with leading zeros; the timezone. The result might be:

<managingEditor>bob@example.com (Bob Jones)</managingEditor>
<webMaster>betty@example.com (Betty Bloggs)</webMaster>
<lastBuildDate>Thu, 03 Sep 2005 16:50:00 GMT</lastBuildDate>

The Date in PHP

To dynamically generate the date in the format for RSS in PHP, you can use this every time you want the date:

date("D, d M Y H:i:s \G\M\T", mktime($date))

Where $date is the date in the standard PHP format - for example:

<pubDate>date("D, d M Y H:i:s \G\M\T", mktime(0, 0, 0, 7, 1, 2000))</pubDate>

Would produce the 1st of July in the format for RSS as the publication date. Note that this assumes the time is GMT - you may need to change the letters accordingly.

If you want to dynamically generate the date in PHP from an entry in MySQL, you will quickly come across a problem - they use different formats. To solve this, you need to convert MySQL dates into PHP dates. Firstly, create a function like so (assuming the date is stored as a datetime):

function mysqltophp($timestamp)
{
$year=substr($timestamp,0,4);
$month=substr($timestamp,5,2);
$day=substr($timestamp,8,2);
$hour=substr($timestamp,11,2);
$minute=substr($timestamp,14,2);
$second=substr($timestamp,17,2);
$phpdate=mktime($hour,$minute,$second,$month,$day,$year);
RETURN ($phpdate);
}

Next, when you want to display the date, you use the same method as before, instead using the function. Lets say the date from a MySQL database is stored in $row[date]; for the publication date, you would use:

<pubDate>date("D, d M Y H:i:s \G\M\T", Mysqltophp($row[date]))</pubDate>

You can use this code whenever you need to insert the date - just change the surrounding tags.

Writing Each Item

Next, we need to create each individual item in the feed. This is relatively simple. Each item is started by <item> and ended with </item>. Inside each item, we have a title, a link and a description. This is similarly self explanatory. In this case, the link should point to the relevant article. For example, an item might look something like this:

<item>
<title>New Bean Eating Record</title>
<link>http://www.example.com/beans/newrecord.html</link>
<description>A man has broken the bean eating record by eating over 20,000 baked beans in under five minutes.</description>
</item>

We can also add a publication date for the item, using the <pubDate> tag. This must be in the correct format: Sat, 07 Sep 2002 00:00:01 GMT. That is, the day in three letters; comma; the day of the month with a leading zero; the year in four digits; the time, including hours, minutes, seconds, all with leading zeros; the timezone. The result could be:

<item>
<title>New Bean Eating Record</title>
<link>http://www.example.com/beans/newrecord.html</link>
<description>A man has broken the bean eating record by eating over 20,000 baked beans in under five minutes.</description>
<pubDate>Thu, 03 Sep 2005 17:00:00 GMT</pubDate>
</item>

You can then add many more items in a similar fashion.

Finishing Touches

We are nearly done now. Once you have added all of the items in the feed, you need to close the channel and RSS feed. You do this by adding:

</channel>
</rss>

The result should be similar to what was displayed a few pages ago. Now, we need to add the feed to the website. We can do this in two ways. The first is embed it in the pages of the websites. In the head of your pages, add this line:

<link rel="alternate" type="application/rss+xml" title="The Title Of Your Feed" href="rss.xml" />

Obviously, the title attibute should be the title of the feed, while the href attribute should point to the feed itself. This feed allows programs such as Opera and Mozilla Firefox to find the feeds. We can also add the feed as a button. First of all, you will need to find an appropiate button for your web page. A quick search on Google shows a multitude of images you could use. When you do use an image, make sure that you upload to your own webspace, and that it is not copyrighted. Common ones to use are the orange buttons with XML and RSS written on them, although, for the sake of continuity, this site has a different button.

Next, put the button on your page, and create a link to the RSS file. The (X)HTML code might look like this:

<a href="rss.xml"><img src="images/rss.png" alt="RSS2.0 Feed" title="RSS Feed" height="15" width="80" /></a>

If you're using HTML, rather than XHTML, you won't need the forward slash at the end of the <img> tag. The various attributes, especially the link, image source and dimensions, will obviously need changing to the correct values.

And that should be it! Users of your site should now be able to find and view and your feed. If the feed doesn't work, make sure you have all the right tags, with correct spelling and cases (as in upper and lower cases), and that each tag is closed correctly.

Useful Links