How to make a dynamic RSS feed in PHP

Wikipedia's entry on RSS tell us that:

RSS (Really Simple Syndication) is a family of web feed formats used to publish frequently updated content including, but not limited to, blog entries, news headlines, and podcasts.

So, it's a file format used to publish content. It's useful because people can easily subscribe to these feeds and receive updated content of their liking when and where they like, from various sources in an organized manner. For an example of a RSS feed you can view this site's feed: Broculos.net RSS feed.

The software used to view RSS feeds is called a RSS reader and can be web based or desktop based. XML is used to specify the RSS format.

A link to the RSS feed is usually found together with a feed icon. More on this later.

Example RSS file

The RSS format is quite simple. In this tutorial we will use the RSS 2.0 Specification. We'll only use a few tags. You should see the specification if you need or want more.

An RSS is composed of a channel and a collection of items. Be aware that there's a maximum number of items you can have in your RSS feed. For example, in version 0.91 of the RSS specification, there's a limite of 15 items. Readers may also impose their own limits.

Here is a very simple example of a RSS file.

<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
    <channel>
        <title>Example RSS feed</title>
        <description>Example of a RSS feed, part of a programming tutorial on making a feed in PHP.</description>
        <link>http://www.broculos.net</link>
        <copyright>Copyright (C) 2008 Broculos.net</copyright>
        <item>
            <title>Example 1</title>
            <description>This is the description of the first example.</description>
            <link>http://www.example.com/example1.html</link>
            <pubDate>Mon, 29 Dec 2008 22:10:00 -0600</pubDate>
        </item>
        <item>
            <title>Example 2</title>
            <description>This is the description of the second example.</description>
            <link>http://www.example.com/example2.html</link>
            <pubDate>Thu, 03 Jan 2008 14:27:15 -0600</pubDate>
        </item>
    </channel>
</rss>

How to make a dynamic RSS feed in PHP

As you can see from the example, it's a very simple file format. But you don't want to make a new RSS file by hand every time something changes in your site.

So, you have 2 options: making a script to produce the RSS file or making a script that is the RSS file. I prefer the later because it's more simple and the first solution requires that you call the script every time you want to update the RSS feed.

Let's make our dynamic always-updated RSS feed. We only need to output the correct tags with content. Because this is just a proof of concept, the data is defined in the file. In a real world example, the content would be loaded from a database.

<?php
 
    /**
     * For demonstration purposes, the data is defined here.
     * In a real scenario it should be loaded from a database.
     */
    $channel = array("title"        => "Example RSS feed",
                     "description"  => "Example of a RSS feed, part of a programming tutorial on making a feed in PHP.",
                     "link"         => "http://www.broculos.net",
                     "copyright"    => "Copyright (C) 2008 Broculos.net");
 
    $items = array(
        array("title"       => "Example 1",
              "description" => "This is the description of the first example.",
              "link"        => "http://www.example.com/example1.html",
              "pubDate"     => date("D, d M Y H:i:s O", mktime(22, 10, 0, 12, 29, 2008)))
        , array("title"       => "Example 2",
                "description" => "This is the description of the second example.",
                "link"        => "http://www.example.com/example2.html",
                "pubDate"     => date("D, d M Y H:i:s O", mktime(14, 27, 15, 1, 3, 2008)))
    );
 
    $output = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $output .= '<rss version="2.0">';
    $output .= "<channel>";
    $output .= "<title>" . $channel["title"] . "</title>";
    $output .= "<description>" . $channel["description"] . "</description>";
    $output .= "<link>" . $channel["link"] . "</link>";
    $output .= "<copyright>" . $channel["copyright"] . "</copyright>";
 
    foreach ($items as $item) {
        $output .= "<item>";
        $output .= "<title>" . $item["title"] . "</title>";
        $output .= "<description>" . $item["description"] . "</description>";
        $output .= "<link>" . $item["link"] . "</link>";
        $output .= "<pubDate>" . $item["pubDate"] . "</pubDate>";
        $output .= "</item>";
    }
    $output .= "</channel>";
    $output .= "</rss>";
 
    header("Content-Type: application/rss+xml; charset=ISO-8859-1");
    echo $output;
 
?>

The second part of the script is the actual making of the RSS feed. It expects a $channel variable and an $items array with its respective values matched to the necessary keys.

After building the XML and storing it in the $output variable we need to display it. Before displaying the actual XML, we send an header indicating the content type of the file. In this case we say it's an RSS XML file. Finally, we output the result.

How to make browsers recognize your RSS feed

Now you should make your RSS feed available for everyone who wants to subscribe to it. Besides putting a link in your site for the RSS feed you should also take on additional step.

There's an HTML tag that you can use to indicate that you have an RSS feed. You should put it between the head tag.

<link rel="alternate" type="application/rss+xml" title="Title of the feed (RSS 2.0)" href="rss.xml" />

You should specify the correct title of your RSS feed as well as its location.

For browsers that are RSS-enabled, this guarantees a simple way for your RSS feed to be detected. They usually indicate that a RSS as been detected by displaying a RSS icon in the location bar.

Conclusion

Don't forget - we only talked about the most basic tags. You should see the complete specification to know better what to include and what to leave out.

The code we produced is not very reusable. You could build a RSS writer with a few classes or turn it into a function.

What more there's left to do? You should use the standard RSS icon, make sure the RSS is valid by validating your RSS feed and for more advanced features for your RSS feed like visitor stats you could use FeedBurner.

Resources

Related articles