"Tell the truth and run." - Yugoslav proverb

Writing An RSS Feed

Friday 9th September 2005

Categories: Guides, Internet, Code

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:


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>
<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:

<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)
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.