Merging RSS Feeds With SimplePie

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).


26 Responses to “Merging RSS Feeds With SimplePie” (Comments RSS)

  1. ladynada
    1:09 am on August 17th, 2007

    Good job! Thank you for sharing this. I am working with simplepie now. Their site is down and I needed some code!

    nada

  2. 186
    6:21 pm on August 20th, 2007

    I have been looking for and trying to do exactly what you describe but with no luck.

    I used your sample and no luck. I changed some of the syntax errors and still wont fire.

    Thank you for posting this.

  3. Matt
    1:50 pm on August 24th, 2007

    Dang, Wordpress is changing my code again…

    It’s changing strait quotes into curly quotes, and things like that. I’ll put-up a downloadable txt file to straighten it out.

  4. Matt
    1:59 pm on August 24th, 2007

    There. You can download the code here. There may be some differences in the coding style, but it’s essentially the same. I cleaned-up my code when I published it in the blog post, but I didn’t have time to do it in the txt file. It works though. Enjoy!

  5. Richard
    3:39 pm on August 28th, 2007

    I’m getting posts shuffled. is there a way to force a sort by post time?

  6. Matt
    8:28 am on August 29th, 2007

    Um… Are the feeds you are merging all RSS 2.0? SimplePie seems to have a bug where they don’t go in the right order if they don’t have the pubDate tag in the feeds.

  7. Richard
    1:13 pm on August 29th, 2007

    It looks like there were incompatible date formats - one atom, one RSS 2.0.

  8. Matt
    2:01 pm on August 29th, 2007

    Can you give me the URLS of the feeds? I’ll look into it.

  9. Guillermo
    4:25 am on November 7th, 2007

    Hi, congratulations and thanks for this great tutorial.
    I have used your code to create a feed merging all contributor feeds. Here you can see it working:
    The Planet: http://www.planetubuntu.es
    The feed: http://www.planetubuntu.es/rss.php

    It’s working fine, but I have a problem with the dates an hours which are shown in the feed reader. In my feed reader, I reload my subscription to PlanetUbuntu and it puts all the new articles with the same date. I have an screenshot: Feed PlanetUbuntu Screenshot.

    Can you help me, please?
    Thank you very much.

  10. Matt
    11:07 am on November 7th, 2007

    Viewing the source of your merged feed (http://www.planetubuntu.es/rss.php), I don’t see any pubDate tags.

  11. venkat
    10:10 pm on February 24th, 2008

    hi… where and how to create smallpie.inc file.. please help me to get that file

  12. Matt
    11:02 pm on February 24th, 2008

    You can find it at http://www.simplepie.org.

  13. fabme
    6:58 pm on March 25th, 2008

    I would like to know where i can find informations about feeds and the law ?
    can i do a website and publish any feeds on it ??

  14. Matt
    10:13 pm on March 25th, 2008

    Well, you’ve touched upon a complicated subject, fabme. If you display feeds in a manner similar to popurls.com (linked titles, maybe a short excerpt), then you’re okay. Most people would not want you to reproduce entire articles though.

  15. fabusdr
    5:59 am on April 17th, 2008

    hello
    thank you for sharing this, it is really useful for me.

    I just suggest you to use simplepie core plugin, instead to load manually the library. as they write on their site:

    http://simplepie.org/wiki/plug.....lepie_core

    Don’t load SimplePie manually by including/requiring simplepie.inc or idna.class.php. Instead, check to see if the SimplePie class is defined. If it is, you’ll know that SimplePie is available to be used.

    Thanks again

    f

  16. Matt
    11:31 am on April 17th, 2008

    fabusdr, thanks for the info. I was unaware of this option. I’ll have to remember it for future use.

    However, it probably wouldn’t work well in this situation. This tutorial is made to work outside of WordPress, as a standalone script.

  17. 186
    7:03 am on May 11th, 2008

    Hello again.Is it me or is there a problem with parsing “Feedburner” feeds?Anytime I include one it does not show and it returns various results with the number of feeds in the output.

  18. Matt
    12:00 pm on May 11th, 2008

    That’s strange. I never have any problem with FeedBurner. Perhaps it has to do with the specific feeds, not Feedburner itself? Try parsing my FeedBurner feed: http://feeds.feedburner.com/Webmaster-source and see if it does anything.

  19. Sam England
    8:15 am on June 9th, 2008

    The script wotks great with these feeds instance:
    http://www.timesonline.co.uk/t.....siness.xml‘,
    http://www.timesonline.co.uk/t.....s/tech.xml

    But not these

    http://feeds.reuters.com/reuters/technologyNews‘,
    http://feeds.reuters.com/reuters/internetNews

    or these

    http://timesofindia.indiatimes.....efault.cms‘,
    http://timesofindia.indiatimes.....479906.cms

    It seems the script will work with feeds with .rss, .xml, extensions

    The script returns nothing when used with scripts with no extensions
    (like reuters use above), or custom extensions like .cms (like
    indiatimes uses above)

    Is there a simple edit to my script, or simplepie.inc that might fix
    this? In simplepie.inc line 12634 I can see this in reference .rss,
    .xml etc extensions but im not sure if it is anything to do with it:

    if (in_array(strtolower(strrchr($value, ‘.’)), array(’.rss’,
    ‘.rdf’, ‘.atom’, ‘.xml’)))

    Thank you in advance for any help

  20. Matt
    12:50 pm on June 9th, 2008

    @Sam, I’ve heard of people having the same problem as you, but have never experienced it myself. I would make sure you have the latest stable version of SimplePie, and if that doesn’t help I’d post a help request on the SimplePie support group here: http://tech.groups.yahoo.com/g.....e-support/

    I haven’t been able to solve the problem for people who have had it before, but I suspect if you post on the SimplePie forum, and include the code you’re using, they will have a solution.

  21. Trackback: links for 2008-06-22 « marka buku
    2:33 pm on June 22nd, 2008

    [...] Merging RSS Feeds With SimplePie | Webmaster-Source (tags: simplepie tutorial) [...]

  22. deuts
    8:31 am on June 25th, 2008

    In the text file, “php” doesn’t really appear after the “<?” ?

  23. deuts
    9:30 am on June 25th, 2008

    with the $item->get_description(), it returns a summary feed. I want to show the whole post. Could you please tell us how to do that? the get_content doesn’t seem to work. thanks.

  24. Matt
    5:21 pm on June 25th, 2008

    @deuts To answer your first question, “<?” is a shortcut for “<?php”. I was a little lazy when I typed out the version seen in the text. It should work on most systems.

    About $item->get_description()… Make sure the input feed is full. If it’s a summary feed, you won’t be able to get a full feed out of it. get_content() is what you need though if you want to output a full feed. Make sure you’re using the proper syntax of echo $item->get_content();

  25. deuts
    7:46 pm on June 25th, 2008

    Hey Matt, thanks for that response. But I just can’t seem to make it to work (the full feed). I was just trying to consolidate the feeds from my blog (WordPress) and photoblog (pixelpost). The pictures in pixelpost was doing fine, but the feeds from WordPress (www.deuts.net) were truncated. I’m nearly giving up….:D

  26. Matt
    8:24 pm on June 27th, 2008

    @deuts. Have you tried posting on the official SimplePie support group? http://tech.groups.yahoo.com/g.....e-support/ Try posting your full code their, along with a detailed description of your problem. They normally find a workable fix pretty fast.

Leave a Reply (or trackback)


Close
E-mail It