Adding RSS Content to Your Hompeage

Your homepage is your representation of who you are: information, pictures, or a general description of who you are. Most homepages have a short bio, a random picture, and a quote or two to go along with it. You'll find links to other content on the site, but it's the kind of page that you write once and forget about. Yet it's the first page your users will come to - so why shouldn't they get updated content when they stop by?

Using MagpieRSS, it's possible to integrate content of an RSS feed into your homepage, if you're managing your content with PHP. No longer must you only have static content on your homepage! No longer must you be the unchanging scourge of the internet! Simply follow these easy steps.

Note, however, that this hack requires you to have an RSS feed of content you want to include. It is generally directed at someone with a seperate section of their website which is updated regularly and offers an RSS feed. This hack will help you mirror your more dynamic content to the homepage of your site, but it can't create content for you!

Installing MagpieRSS

First step to this hack is to download MagpieRSS from the MagpieRSS homepage, Once you've downloaded the latest release from Sourceforge, you will need to create a directory in your webroot to store the include files that MagpieRSS needs. The documentation suggests a ``magpierss'' directory.

Once you've created this directory, copy the four include files (,,, and, and the directory extlib (which contains a modified version of the Snoopy HTTP client) into the directory.

That's it. You've installed the MagpieRSS library.

Basic Use of Magpie

First step is to include the MagpieRSS library in your PHP page. If you created a ``magpierss'' folder in your web directory, and your homepage is at the root of that structure, you can simply use the line:


Once you've done this, you can parse an RSS feed into a variable:

    $rss = fetch_rss("";);

Once you've done this, you can iterate over the items, printing a title and link for each of them:

    echo "<ul>";
    foreach ($rss->items as $item) {
        $title = $item['title'];
        $href = $item['link'];
        echo "<li><a href='$href'>$title</a></li>";
    echo "</ul>";

Simple! You now have a list of the entries in your RSS feed - useful if you simply want an up-to-date sidebar, but not the most useful thing if you want to provide a quick summary of some of the more recent items, rather than just a link.

So, what if you want to include the content of the last post, and then the latest three headlines, instead of just the headlines for each?

Advanced Use

If you want to display a different kind of text for the first entry than the rest of the entries parsed from the RSS feed, the first step is to seperate that entry out, and print the entry as you want it. For my RSS feed, I display the <description> tag, which has a shortened version of the content:

    $item = $rss->items[0];
    $title = $item['title'];
    $href = $item['link'];
    echo "
        <a href='$href'>$title</a>
        <br />".$item['description']." <a href='$href'>[more]</a>

With this, we get a header, and a small description in a paragraph layout. Next, we want to take four more entries out of the RSS entry array, so that we can use the loop from above over them, creating a list of entries.

    $rss = array_slice($rss->items, 1, 5);
    echo "<ul>";
    foreach ($rss as $item) {
        $title = $item['title'];
        $href = $item['link'];
        echo "<li><a href='$href'>$title</a></li>";
    echo "</ul>";

This will generate the list from above, with the first through fifth items. This allows us to display the first five items from the RSS feed, in a way that's more appealing and provides more information to the casual reader than simple links do.

Although the examples here are based around a simple RSS 1.0 feed, they should work just as well with most RSS 2.0 feeds. In addition, it's possible to use other elements included in the RSS feed, whether they are extended via namespaces or in some other way. RSS 1.0 feeds often include a dc:date property for the time the entry was made: using Magpie, this can be accessed as $item['dc']['date']. Magpie offers a built-in parser for the W3C Date/Time Format, which can be used to turn this element into a usable timestamp:

    $time = parse_w3cdtf($item['dc']['date']);
    $date = date("D M j G:i:s T Y", $time);

The date variable now holds a date string of the form ``Sat Aug 27 4:22:00 EDT 2005''. A similar method can be used against valid RSS 2.0 feeds which include the pubDate element: this element uses the W3C DTF for representation of the date and time as well.

Extending the Hack

Once your website receives a significant amount of load, you may notice that constantly parsing the RSS feed for information is too many hits on your RSS feed. Magpie offers built in caching for this case. By default, Magpie will attempt to cache parsed RSS data in a ``cache'' directory directly below the location of the PHP file calling rss_fetch. This directory must exist, and be writable by the webserver, in order for Magpie to successfully use this cache.

If you can not create a webserver-writable cache directory in this location, it is possible to assign a different cache directory, by defining the MAGPIE_CACHE_DIR variable.

    define('MAGPIE_CACHE_DIR', '/tmp/magpie_cache');

Note that this file will contain all the information the RSS feed does, so if it is designed to be private or otherwise protected, you will need to take the same precautions in protecting the cache directory as you do for the content displayed on the web.

Never again be caught with static, out of date content on your homepage! Use Magepie to fetch and display RSS information on your homepage, and keep everything on the site in sync!