Easy RSS Syndication with MagpieRSS

What is MagpieRSS?

MagpieRSS, affectionately known as Magpie, is an RSS and Atom parser for PHP. It allows you to display the newest articles from websites with RSS or Atom feeds on your own site. This is a great way to add new, fresh, and relevant information to your site. It parses RSS 0.9, RSS 1.0, and has some support for RSS 2.0 as well as Atom 0.3.

What do I need to use Magpie?

In order to use Magpie your server must support PHP version 4.2.0 with XML support, or PHP5 with libxml2 support. After that, setting up and using Magpie is a cinch.

Okay, I've heard enough, where do I get it?

You can download Magpie from SourceForge by clicking here.

Now what?

Simply extract the file and upload the following files to your server: rss_fetch.inc, rss_parser.inc, rss_cache.inc, and rss_utils.inc and the entire directory named extlib. You can put theses files in any directory in the root of your webserver. I tend to upload them to a directory named rss.

Next, you need to decide where you'd like the syndicated content to go. For example, if you wanted to syndicate content on the index of www.example.com you would need to edit the index.html(php) file. If your file doesn't end in php, you will need to change it so it does, as we will be adding php code to it.

Next we will actually edit the file index.php. We need to add the following code to the top of the page you want to display the feeds on:

<?php require_once('rss/rss_fetch.inc'); ?>

Please note that your path may be different depending on what you named the directory, and where in the tree your index.php resides. To actually add the feeds to your page you also need to add the following lines to fetch and parse the RSS:

<?php $rss = fetch_rss("http://site.to.syndicate.com/index.rss"); ?>

and replace the address with the address of the feed you wish to display.

This will return an array, $rss that includes the syndicated content as well as some information about the publisher. The other information returned (usually) is the name of the publisher, stored in $rss->channel['title'] , a description of the publisher stored in $rss->channel['description'] , and a general link to the publisher stored in $rss->channel['link'] .

The items can be accessed through $rss->items and a simple loop can be used to transverse the items one at a time. The code for the loop is:

foreach ($rss->items as $item)
{ // Code to manipulate the items here
}

Each item contains a title, link, and description. $item['title'] contains the title of the article, $item['link'] is the link to the original and $item['description'] is the description of the story which is often the first few lines of content. A basic list of the syndicated content can be achieved using the following code:

<?php require_once('rss/rss_fetch.inc');
$rss = fetch_rss('http://site.to.syndicate.com/index.rss');

echo "<a href=".$rss->channel['link']."><b>".$rss->channel['title']."</b></a>";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$desc = $item['description'];
echo "
<p><a href="$href">$title</a><br>
";
if($desc) echo $desc;
}?>

This is pretty basic, but you should be able to customize it to fit your needs.

General Problems and Solutions

Syndicated content is too long for my site.

If the content you are syndicating is too long or conatins too many items and begins to make your site look funny you can simply add the following lines to set the amount of entries displayed and set the length of the description.

$items = array_slice($rss->items, 0, 5);

This line returns only a subset of the feed, in this case only the first 5 entries. To shorten the desription you would use the following lines:

if (strlen($desc) >= 100)
{
$desc = substr($desc,0,99)."...";
}

What this does is check if the string length for the description is longer then 100 characters, which if it is, will display the first 99 and ... afterwards to let the reader know it continues.

I would like to enable caching of feeds to reduce bandwith and speed up loading. Is this possible?

You bet! Magpie has caching enabled by default, but you will probably need to change the directory it caches into as well as making it writeable by the user the server runs as. It tries to write to the working directory in which the script is called. For example, if you add the feed to your home page, the script attempts to create the cache directory in the root of your server. The following lines enable the cache and set the directory it caches to:

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

By default Magpie only caches items for one hour, but this can be changed with the following line:

define('MAGPIE_CACHE_AGE', 1800);

This sets Magpie to cache items for 30 minutes. It's important to note that the interval is specified in seconds.

Okay, I added those lines, but the caching still isn't working!

You need to make sure that the directory you set the cache to exists and is writeable by the user the server runs as. In Debian this is www-data. I'm sorry, but I don't know which user apache runs as by default on other distributions. You can accomplish that with the following commands (as root):

mkdir /tmp/magpie_cache
chown www-data:www-data /tmp/magpie_cache

Please note that the cache doesn't have to be in /tmp and is actually a good idea to change if you use shared hosting. After that refresh the page and you should see some files with some very odd names in the cache directory. If you do all is well. If not, please refer to the FAQ on the MagpieRSS Homepage.

Putting it all together

The following is how the options should be combined, and is a working example with minor modifications from my own site.

<?php
define('MAGPIE_CACHE_DIR', '/var/cache');
$rss = fetch_rss('http://digg.com/rss/indexlinux_unix.xml');
$items = array_slice($rss->items, 0, 5);
foreach ($items as $item) {
$href = $item['link'];
$title = $item['title'];
$desc = $item['description'];
echo "
<p><a href="$href">$title</a><br>
";
if($desc)
if (strlen($desc) >= 125)
{
$desc = substr($desc,0,124)."...";
}
echo $desc;
}
;
?>

This retrieves the feed from www.digg.com in the Linux section and displays the first 5 entries with a 125 character long description. It also caches to the directory /var/cache with the default cache time of 1 hour.

And there you have it, quick and easy RSS syndication with MagpieRSS! If you need more information, help, or anything else I strongly encourage you to check out the MagpieRSS Homepage. They also have an active mailing list and maintain archives that have answered my questions everytime I've needed help.

Share this page:

4 Comment(s)