Jan Leow's Press Blog


Combining multiple RSS feed into one output

Ever since Yahoo Pipes service was shut down, I’ve been looking for a good alternative to merge my various RSS feeds. My website is composed of several sections using WordPress, Google Blogger and Tumblr. I have tried some free services out there but they were never as good as Yahoo Pipes. And without a good monetizing idea, those services too, died in the end. Leaving me in the lurch yet once again. Finally I decided to search out some code that would sit in my own web server and never have to worry about such services shutting down.

My first try was a code that I found called Feedcreator using magpie. It could handle feeds created by WordPress and Tumblr but not Google Blogger. I tried several workarounds including passing the Blogger feeds into Feedburner and inserting it back to Feedcreator, but it didn’t work.

Then it occurred to me that Simplepie may just get the job done. Currently I’m using it to combine my blog output onto my homepage. So it is just a matter of finding the code sequence. Unfortunately Simplepie site documentation weren’t very clear on how to do it, but I managed to eventually track down a few sites giving examples on how to get it done.

First off, the command code to output the combined RSS feed is:

  enable_xml_dump(isset($_GET[‘xmldump’]) ? true : false)


This will dump the output as an XML formatted data to be later echoed out with other Simplepie commands. One of the author suggested using Feedburner to created a much nicer looking formatting after creating the RSS XML file which suits me just fine.

Without much ado, here’s my example after modifying it to suit my needs. You may want to customize it further for your purposes. You will need to download the simplepie.inc file and placed it somewhere in your web server for this to work and of course a writable cache folder for simpiepie to store its temporary output.



<?php
/****************************************************
     RSS FEED USING SIMPLEPIE XML DUMP
****************************************************/
require_once(‘./simplepie_folder/simplepie.inc’);//INCLUDE EXTERNAL LIBRARIES
header(‘Content-Type: application/rss+xml; charset=UTF-8’); // SET HTTP HEADERS
?>

<rss version=”2.00″>
<channel>
<title>Jan Leow Homepage</title>
<link>http://www.janleow.com</link>
<description>Jan Leow RSS Feed Combined</description>
<language>en-us</language>


<?php
$feed = new SimplePie(); // Create a new instance of SimplePie
// Load the feeds
$feed->set_feed_url(array(
‘http://tumblr.janleow.com/rss’,
‘http://go.janleow.com/feeds/posts/default’,
‘http://www.janleow.com/life/feed’,
‘http://web.janleow.com/photography-basics/feed’
));
$feed->set_cache_duration (600); // Set the cache time
$feed->enable_xml_dump(isset($_GET[‘xmldump’]) ? true : false); // output as raw XML
$success = $feed->init(); // Initialize SimplePie
$feed->handle_content_type(); // Take care of the character encoding
?>


<?php
// limit loop for the output
if ($success) {
$itemlimit=0;
foreach($feed->get_items() as $item) {
if ($itemlimit==20) { break; }
?>


<item>
<title><?php echo $item->get_title(); ?></title>
<link><?php echo $item->get_permalink(); ?></link>
<guid><?php echo $item->get_permalink(); ?></guid>
<description><?php echo $item->get_description(); ?></description>
<pubDate><?php echo $item->get_date(‘j F Y | g:i a’); ?></pubDate>
</item>

<?
$itemlimit = $itemlimit + 1;
}
}
?>
</channel>
</rss>

Leave a Comment

Your email address will not be published. Required fields are marked *

Blue Captcha Image
Refresh

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.