Programming a MySQL Fed RSS Feed in PHP: Part 1

Posted on Thu, 03 May 2007
 at 10:03:48

     RSS, or Really Simple Syndication, is a massively popular tool on the internet - for a visitor it's a great way to stay update on news and updates of your favorite sites, and for a site owner it's a great way to get repeat traffic to your site, and in this post I am going to show you how to create one that takes it's content directly from a MySQL database using PHP.

     Now your table layout will vary to mine, so to keep it simple for this tutorial I will use this layout:

> ID (Primary Key)
> Title
> Description
> URL
> Date (25 Chars)


     The field title names speak for themselves - `Title` will be the title we use in the feed, `Description` will be what is displayed as the description in the feed, `URL` is the address the feed will point to and `Date` is the RFC 2822 formatted date, which is important.

     Now this database layout can be changed as you see fit - for Dan Blog I have the RSS builder use the same table that I post my posts in, so the table can also contain the content you wish to display and whatever else you want. I haven't included that here because we won't be using it so there's no point.

     Now the date field requires a little explanation here. I have stated above the the date field should be 25 chars long, but the RFC 2822 date can have up to 31 characters - I have done this simply because the last 6 are not helpful in this context and so a smaller field size beens they will not be saved.

     OK - this program has 2 files: rss.php (the page the client uses) and rss.class.php, which contains the bulk of the code that builds the feed.

     rss.php looks like this:

<?php
    header("Content-Type: application/xml; charset=ISO-8859-1");
    require_once("rss.class.php");
    $feed = new RSS;
    print "<?xml version=\"1.0\"?>";
    print "<rss version=\"2.0\">";
    print "<channel>";
    print '<title>SITE TITLE</title>
            <link>SITE URL</link>
            <description>SITE DESCRIPTION</description>
            <language>en-uk</language>';
    print '<webMaster>WEBMASTER EMAIL</webMaster>';
    print "<lastBuildDate>".$feed->getLastBuildDate()."</lastBuildDate>";
    $feedval = $feed->generatefeed();
    // Print out the feed
    if(!$feedval) {
        // Error
    }
    print $feedval;
    print "</channel></rss>";
?>

     OK, now we shall break this down into it's different parts.

> header("Content-Type: application/xml; charset=ISO-8859-1"); This line of code sends headers to the visitors browser telling it that the page should be treated as XML and also what charset to use. Simple, but vital.

> require_once("rss.class.php"); This includes the RSS class which I will show to you and explain in part 2 - this class handles formatting the feed items, as well as some other RSS related functions.

> $feed = new RSS; Creates an instance of the RSS class.

     Now the next few lines begin the output. The first 3 lines do not need you to do anything, but after that you need to fill in the title, link, description, managingEditor and webMaster tags with details of your web site.

> print "<lastBuildDate>".$feed->getLastBuildDate()."</lastBuildDate>"; This line prints out the lastBuildDate tag which uses the RSS class. This tells any aggregators the last time your feed was updated, and I will show you this function next time.

> $feedval = $feed->generatefeed(); This fills the variable $feedval with the feed; this also uses the RSS class.

     The next few lines are used for error catching. I have simply left the if statement blank so the feed generated will be empty.

> print $feedval; This prints out the feed items.

> print "</channel></rss>"; and this finishes off the RSS page by closing the RSS equivalent of the body and html tags respectively.

     So not too difficult so far. This page, as I have said, is what the client sees, so, to make it available put this between the head tags:-

<link rel="alternate" type="application/rss+xml" title="FEED TITLE" href="FEED URL" />

...and replace FEED TITLE with your feed title and FEED URL with the path to your feed.

     Part 2 is here.

Add this page to Stumble.

There are 0 Comments Below | Add a Comment

Sorry, now there's advertising.