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

Get How to Build an RSS 2.0 Feed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.