Have you played around with SimplePie? If not, start experimenting. SimplePie is an amazingly useful file you include in your PHP scripts so you can parse RSS feeds. It’s fast, it’s powerful, it’s easy to use, and there’s a lot you can do with it.
In addition to the ordinary stuff, like displaying feed content on your website, you can also utilize SimplePie for more interesting things….like combining RSS feeds. Why would you want to do that? Well, some of us have multiple blogs. Using SimplePie you can create a feed that aggregates all of your blogs’ feeds. Blog networks may find this technique useful as well.
How do you merge feeds? It’s actually quite simple. Your script should do the following:
- Echo the beginning of the XML (the character encoding must be UTF-8)
- Load simplepie.inc
- Store all the original feeds in an array
- Initialize SimplePie
- Loop around echoing the aggregated RSS items
- Echo the end of the XML
Not too complex, eh? Let’s get coding!
First you need to output the beginning of the XML structure
<?php echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>'; ?>
<rss version=”2.00″>
<channel>
<title>Aggregated Feed Demo</title>
<link>http://www.webmaster-source.com</link>
<description>
A demo of SimplePie’s feed-merging capabilities.
</description>
<language>en-us</language>
Just basic XML. Note that you need to use UTF-8 encoding, otherwise you could have some oddities in the feed. SimplePie prefers UTF-8, and can be a little buggy with other encodings.
Next, the script takes care of some SimplePie stuff and loads the RSS feeds.
<?php
include_once(‘./lib/php/simplepie.inc’); // Include SimplePie
$feed = new SimplePie(); // Create a new instance of SimplePie
// Load the feeds
$feed->set_feed_url(array(
‘http://feeds.feedburner.com/Webmaster-source’,
‘http://feeds.feedburner.com/ProbloggerHelpingBloggersEarnMoney’
));
$feed->set_cache_duration (600); // Set the cache time
$feed->enable_xml_dump(isset($_GET[‘xmldump’]) ? true : false);
$success = $feed->init(); // Initialize SimplePie
$feed->handle_content_type(); // Take care of the character encoding
?>
The only things you really need to change above are the feed URLs and the path to simplepie.inc. Now for the fun part. Let’s output ten RSS items, shall we?
<?php if ($success) {
$itemlimit=0;
foreach($feed->get_items() as $item) {
if ($itemlimit==10) { break; }
?>
<item>
<title><?php echo $item->get_title(); ?></title>
<link><?php echo $item->get_permalink(); ?></link>
<description>
<?php echo $item->get_description(); ?>
</description>
</item>
<?
$itemlimit = $itemlimit + 1;
}
}
?>
</channel>
</rss>
That was easy, wasn’t it? Just save it as rss_all.php, or something similar, and upload it to your web server. Try it out. Note that it’s a good idea to use Feedburner so your server doesn’t have to process everything when a user’s RSS reader requests the feed. The SimplePie website has a complete API reference you may find useful. With a couple minutes of coding, you can easily add a <pubDate> tag to the generated feed. I highly recommend doing so, as it will make it easier for others to mix your aggregated feed with some of theirs.
Want to see this script in action? Take a look at this feed. It has the ten most recent items from the NTugo Blogs feeds
Download TXT file (copy-pasting code from blog post may not work).
Pingback: links for 2008-06-22 « marka buku
Pingback: links for 2008-07-24 » Oliver Willis
Pingback: Merging RSS Feeds With SimplePie | Webmaster-Source – auguag
Pingback: 14 Tutorials to Get You into SimplePie - The Designed
Pingback: benjamin junior » Blog Archive » Bookmarks em August 12th