Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge multiple RSS feeds with SimplePie
<?php
/* Merge multiple RSS feeds with SimplePie
*
* Just modify the path to SimplePie and
* modify the $feeds array with the feeds you want
*
* You should probably also change the channel title, link and description,
* plus I added a CC license you may not want
*
* Help from: http://www.webmaster-source.com/2007/08/06/merging-rss-feeds-with-simplepie/
*
*/
header('Content-Type: application/rss+xml; charset=UTF-8');
// Your path to simplepie
include_once('/path/to/simplepie/simplepie.inc'); // Include SimplePie
//
$feedlink = "http://somedomain.com/thisfeed/"; // URL for this feed, <atom:link>
$feedtitle = "Jon's Feeds"; // <title>
$feedhome = "http://jon.smajda.com"; // <link>
$feeddesc = "One Feed to Aggregate Them All"; // <link>
// Feeds you want to aggregate
$feeds = array(
'http://jon.smajda.com/rss.xml',
'http://smajda.tumblr.com/rss',
'http://files.smajda.com/jon/feeds/greader/',
'http://twitter.com/statuses/user_timeline/14285636.rss',
'http://feeds.delicious.com/v2/rss/smajda',
'http://contexts.org/howto/feed/',
'http://github.com/smajda.atom'
);
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>
<channel>
<title><?php echo $feedtitle; ?></title>
<atom:link href="<?php echo $feedlink; ?>" rel="self" type="application/rss+xml" />
<link><?php echo $feedhome; ?></link>
<description><?php echo $feeddesc; ?></description>
<language>en-us</language>
<copyright>Copyright <?php echo '2007-'.date("Y"); ?></copyright>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
<?php
date_default_timezone_set('America/Chicago');
$feed = new SimplePie(); // Create a new instance of SimplePie
// Load the feeds
$feed->set_feed_url($feeds);
$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
?>
<?php if ($success) {
$itemlimit=0;
foreach($feed->get_items() as $item) {
if ($itemlimit==40) { break; }
?>
<item>
<title><?php echo $item->get_title(); ?></title>
<link><?php echo $item->get_permalink(); ?></link>
<guid><?php echo $item->get_permalink(); ?></guid>
<pubDate><?php echo $item->get_date('D, d M Y H:i:s T'); ?></pubDate>
<dc:creator><?php if ($author = $item->get_author()) { echo $author->get_name()." at "; }; ?><?php if ($feed_title = $item->get_feed()->get_title()) {echo $feed_title;}?></dc:creator>
<description>
<?php echo htmlspecialchars(strip_tags($item->get_description())); ?>
</description>
<content:encoded><![CDATA[<?php echo $item->get_content(); ?>]]></content:encoded>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
</item>
<?
$itemlimit = $itemlimit + 1;
}
}
?>
</channel>
</rss>
@ecocity

This comment has been minimized.

Copy link

commented Jul 23, 2010

thank you, this worked perfectly for submitting a combined feed to Feedburner. I'll post any code tweaks I might use.

@smajda

This comment has been minimized.

Copy link
Owner Author

commented Jul 23, 2010

Cool - thanks!

@Freshpeel

This comment has been minimized.

Copy link

commented Oct 27, 2010

This looks like it will do exactly what I'm looking for!

However, I added my feeds and uploaded this php file, but when I go to the file in my browser to execute it, my browser asks me to save the php file. Other php pages are working fine. Any ideas?

@smajda

This comment has been minimized.

Copy link
Owner Author

commented Oct 27, 2010

This suggests it may be a server timeout issue? Do your php error logs say anything?

@ecocity

This comment has been minimized.

Copy link

commented Oct 27, 2010

you may have to tell htaccess to allow it to execute, or run a simple page with one liner:
to see if php ios executing at all.

@Freshpeel

This comment has been minimized.

Copy link

commented Oct 28, 2010

Here was the result from the simple page: http://freshpeel.com/test.php

Anything here that points to the problem?

@smajda

This comment has been minimized.

Copy link
Owner Author

commented Oct 28, 2010

It's probably a php timeout issue. Your max_execution_time is set to 30. How many feeds are you trying to parse? You can try modifying the timeout, but your host may or may not let you do that. Try adding set_time_limit(0); and see if that works.

@Freshpeel

This comment has been minimized.

Copy link

commented Oct 28, 2010

I added set_time_limit(0); and still get the same result. I'm trying to parse 3 feeds, but one of them is a Google Shared atom feed, which I've had trouble with in the past.

@smajda

This comment has been minimized.

Copy link
Owner Author

commented Oct 28, 2010

Does it work if you take that feed out?

@Freshpeel

This comment has been minimized.

Copy link

commented Oct 28, 2010

No. Removed it. Same result. It's odd because I have Wordpress and other things running with php with no problems.

@smajda

This comment has been minimized.

Copy link
Owner Author

commented Oct 28, 2010

Only thing I can think is to check your php error logs to see if its generating an error each time. If you want, I can take a look (send me a private a message if you don't want to post it here).

@Freshpeel

This comment has been minimized.

Copy link

commented Oct 28, 2010

Ok. Let me see if I can locate it. Thanks for your help.

@Freshpeel

This comment has been minimized.

Copy link

commented Oct 28, 2010

I'm having trouble locating the error log. Not sure where GoDaddy hosts them.

Maybe It's me. May I'm just not utilizing the script right. Should this be a part of an HTML page? Is there somewhere that I should put this to make it work correctly? And where will the merged feed be output?

@tbarnes

This comment has been minimized.

Copy link

commented May 24, 2013

Thanks man. I found another post on this before and it worked (sorta), but I was having some issues in creating some MailChimp RSS-based email campaigns. The other code was buggy, but this worked perfectly! Cheers!

@a4academics

This comment has been minimized.

Copy link

commented Aug 13, 2014

I had used the code to merge the RSS feeds of my Joomla Site. It works fine. I want to modify the order of articles in the feed.

Currently feeds from multiple urls are mixed and sorted by date desc. I want to sort the Category feed URLS in $feeds and then by date desc. Can anyone help me out in doing the same.

@luannebe

This comment has been minimized.

Copy link

commented Oct 25, 2015

Hi,
wonderful script ... thanks so much!
I'm attempting to merge two WordPress RSS feeds into one. The script works perfectly until a post title has multiple special characters in it, specifically a registration mark and and ampersand. Oddly multiple registration marks are ok.

I have set charset=UTF-8 when sending header and set $feed->handle_content_type();

For example, the native feed produces these two item titles:

<title>Companies’ BrandName® and ProductName® Materials Win “Best Component” Award at Trade Show!</title> <title>BrandName® Institute Opens at Company R&D Center</title>

But when run thru simplepie get_title(); ?>
the results are oddly the following:

<title>Companies’ BrandName® and ProductName® Materials Win “Best Component” Award at Trade Show!</title> <title>BrandName® Institute Opens at Company R&D Center</title>

The html entity (®) in the second title causes the feed to fail validation and breaks downstream scripts.

I can get the feed to validate by enclosing the title in CDATA

<title>get_title(); ?>]]></title>

But that seems less than perfect. Is this the best approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.