Created
October 7, 2009 16:41
-
-
Save smajda/204194 to your computer and use it in GitHub Desktop.
Merge multiple RSS feeds with SimplePie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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(); ?>
<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 results are oddly the following:
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?