Using PHP to integrate RSS feeds into your website

PHP can be used to easily include feeds from other sites. The main advantage over a JavaScript solution is that everything can be run from your own site and you can take advantage of caching. The PHP can output straight HTML, which is fully searchable by search engines. As well, since all the files are "local" (except for updates to the included feeds), display tends to be faster, not relying on a JavaScript call to an external site for every page view.

I've chosen to use MagpieRSS for my example, mainly because it is very, very easy to use while at the same time being quite feature rich. It is of course GPL, so you can use and extend it to your heart's content.

It should work "out of the box". Download the magpierss archive to your server space and decompress it. Now, go to the magpie_simple.php file (which should be at http://www.example.com/magpierss-0.5.2/scripts/magpie_simple.php) and enter in the URL of a feed. Ta-da! You've got a feed displayed.

You'll want to add all the settings and includes in one file, so that your feed can easily be included in any page by just using a simple line or two of PHP. From the Magpie site, here is a really simple example of all the code you need:

require_once 'rss_fetch.inc';

$url = 'http://magpie.sf.net/samples/imc.1-0.rdf';
$rss = fetch_rss($url);

echo "Site: ", $rss->channel['title'], "<br>\n";
foreach ($rss->items as $item ) {
	$title = $item[title];
	$url   = $item[link];
	echo "<a href=$url>$title</a></li><br>\n";
}

For the complete example, including the walk-through of uploading files and setting permissions, see the specific example for OpenSourceXperts.com.

This is part of the long term Archive, originally published on

Categories: RSS, MagpieRSS, PHP

Tags:

Last modified at