What are RSS and Atom Feeds and how do I create them?

November 25th, 2009

What are feeds?

Really Simple Syndication (RSS) and Atom feeds are different formats used to publish updates or changes such as new blog entries, news entries or user activity in a standard format. The feed is basically an XML document containing certain information about the updated content.

What information do they contain?

The “feed”, “web feed” or “channel” includes full or summarised text, plus metadata such as published date, category and authorship.

Should I use RSS or Atom?

RSS is more widely used as it was around first although the Atom feed standards seen to have a better structure I personally would stick with RSS 2.0 as it will work on all readers and software, and people are familiar with the term RSS.

Why should I use a feed on my website?

Web feeds make it easy to inform people of changes to your website. Anyone interested in your feed can subscribe to it via an RSS reader which can then keep track of updates for you. You can use fees in outlook, through the browser or through many desktop applications available.

How can I create a feed?

I will talk you through a very basic example of a feed to get you started but if your interested in taking it further you should read the RSS 2.0 specs and also those for Atom feeds.

You can either generate the feed manually or dynamically, but either way the end result will be structured the same.

Below is an example of a RSS document (XML).

<rss version="2.0">
  <title>Social Addict Feed</title>
  <description>This shows the latest updates on socialaddict.co.uk</description>
    <title>HOUSJUNKI :: BOXING DAY :: 26.12.09 :: CLUB.V</title>
    <description>Event added: HOUSJUNKI :: BOXING DAY :: 26.12.09 :: CLUB.V @ Club Venus by andoi - Sat 21st Nov 2009 8:19pm</description>
    <pubDate>Sat, 21 Nov 2009 20:19:40 +0000</pubDate>
    <copyright>(c) Social Addict 2009</copyright>

Creating a static feed

Simply take the code you have produced like above and save it as an XML document, rss.xml will be fine and then upload to your website. You take give out a direct link to the feed or if you would like users of the website to pick up that it exists automatically then simply add the following code into the head area of the HTML code.

<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="rss.xml" />

Dynamically creating a feed using PHP

If you have a dynamic (database driven) website then it would make more sense to dynamically generate the feed.

We have recently made a feed for a client site ( www.socialaddict.co.uk/rss ) and we had to bring together the data from several different tables and merge them so the feed could take all updates into account.

We used the RSS2Writer class to generate the XML document which simplies the use of the PHP XMLWriter object. Grab yourself a copy of the class and you can use the example below to help get you started.

This is assuming you have a mySQL database with a table called “updates” and it contains:

`title` VARCHAR
`category` VARCHAR
`info` VARCHAR
`created` DATETIME

try {
   // Create RSS writer
   $rss = new rss2writer('Social Addict Feed', 'This shows the latest updates on    socialaddict.co.uk', 'http://www.socialaddict.co.uk');
    // Create Database connection
    $dbh = new PDO('mysql:host=localhost;dbname=test', 'username, 'password'); // put your database details in here
    // Get data and iterate through it
    foreach($dbh->query('SELECT `title`,`category`,`info`,`url`,`created` from updates') as $feed) {
        // Get each feed item information
	$rss->addItem($feed['title'], $feed['info'], $feed['url'], true);
	$rss->addElement('category', $feed['category']);
	$rss->addElement('pubDate', date("r",strtotime($feed['created'])));
	$rss->addElement('copyright', '(c) Social Addict 2009');
    $dbh = null;
    // Set header type to XML
    header('Content-type: text/xml');
   //$rss->writeToFile('rss.xml'); //write the xml output to file (optional)
   echo $rss->getXML(); // Output XML to browser / screen
// Catch any errors
} catch (PDOException $e) {
    print "An error occured: " . $e->getMessage() . "<br/>";

Any questions are welcome, and we also help people out where we can.