O'Reilly logo

How to Build an RSS 2.0 Feed by Mark Woodman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building RSS in Code

There are a myriad of ways to build an RSS feed with a scripting/programming language. While we can't teach you how to become a proficient programmer, we can provide sample code in a couple of languages to help you get started.

PHP 4

PHP is a very common scripting language used in web site development. This sample PHP script demonstrates one approach to generating an RSS feed:

<?php

// Declare variables
$channelTitle='Seize Her Salad';
$channelDescription='Yummy recipes and tasty tips from Seize Her Salad';
$channelLink='http://seizehersalad.com';
$channelPubDate='Sun, 05 Nov 2006 00:00:01 −0600';
$itemTitle='Fresh Food Tip';
$itemLink='http://seizehersalad.com/recipe?item=234';
$itemDescription="Today's fresh food tip from Seize Her Salad:  Want an easy way to
lose weight?  Cut out white flour &amp; white sugar from your diet.  This is also an
excellent way to reduce your risk of adult-onset diabetes!";
$itemPubDate='Sun, 05 Nov 2006 00:00:01 −0600';

// Build RSS
$rss = <<<EOD
   <rss version="1.0">
      <channel>
         <title>{$channelTitle}</title>
         <description>{$channelDescription}</description>
         <link>{$channelLink}</link>
         <pubDate>{$channelPubDate}</pubDate>
         <item>
            <title>{$itemTitle}</title>
            <link>{$itemLink}</link>
            <description>{$itemDescription}</description>
            <pubDate>{$itemPubDate}</pubDate>
         </item>
      </channel>
   </rss>
EOD;

// Display RSS
  header('Content-type: text/xml');
  echo "<?xml version='1.0' encoding='UTF-8'?>\n";
  echo $rss;
?>

The above script has "hard-coded" variables ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required