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
  • 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″>
<title>Aggregated Feed Demo</title>
A demo of SimplePie’s feed-merging capabilities.

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.

include_once(‘./lib/php/’); // Include SimplePie

$feed = new SimplePie(); // Create a new instance of SimplePie

// Load the 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

The only things you really need to change above are the feed URLs and the path to Now for the fun part. Let’s output ten RSS items, shall we?

<?php if ($success) {
foreach($feed->get_items() as $item) {
if ($itemlimit==10) { break; }

<title><?php echo $item->get_title(); ?></title>
<link><?php echo $item->get_permalink(); ?></link>
<?php echo $item->get_description(); ?>

$itemlimit = $itemlimit + 1;


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

  • ladynada

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


  • 186

    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.

  • Matt

    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.

  • Matt

    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!

  • Richard

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

  • Matt

    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.

  • Richard

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

  • Matt

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

  • Guillermo

    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:
    The feed:

    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.

  • Matt

    Viewing the source of your merged feed (, I don’t see any pubDate tags.

  • venkat

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

  • Matt

    You can find it at

  • fabme

    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 ??

  • Matt

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

  • fabusdr

    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:

    Don’t load SimplePie manually by including/requiring 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


  • Matt

    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.

  • 186

    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.

  • Matt

    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: and see if it does anything.

  • Sam England

    The script wotks great with these feeds instance:‘,

    But not these‘,

    or these


    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 that might fix
    this? In 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

  • Matt

    @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:

    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.

  • Pingback: links for 2008-06-22 « marka buku

  • deuts

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

  • deuts

    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.

  • Matt

    @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();

  • deuts

    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 ( were truncated. I’m nearly giving up….:D

  • Matt

    @deuts. Have you tried posting on the official SimplePie support group? Try posting your full code their, along with a detailed description of your problem. They normally find a workable fix pretty fast.

  • Frog

    I have been trying to add multiple feeds, but just entering another feed doesn’t work.I have a probleem too with the feed url “feed://“. All the other feed url’s are working, but that one doesn’t.My feed url is ““.

  • Matt

    @Frog Make sure you use the correct syntax. Compare it to the example above and make sure you have the array, the commas, and the quote marks.

  • Frog

    Oeps, I had forgotten the commas (is this correct english).I now have two problems left.One is that the feed doesn’t work anymore when I add feed:// My other problem is that I don’t get more than 10 posts in the feed. Changing $itemlimit==10 doesn’t solve the problem.Do you know how I can solve both problems?

  • Frog

    Oké, I now have 50 posts, but I have still the problem with the above feed. I too have a probleem with the pubDate tag. When I add it my feed only shows 1 post, instead of 50 posts.

    Do you have an idea how I can solve the problems?

  • Pingback: links for 2008-07-24 » Oliver Willis

  • Frog

    Nobody haves an idea?

  • Matt

    @Frog. I’m not sure what your problem is exactly. I’d recommend trying the official SimplePie support group

  • Frog

    Ok, I will try it there.Can you maby post an example of the pubDate tag in this script. Maby i’m just trying it the wrong way.

  • TechHairBall

    This is a wonderful tool. I used it on my blog and now working on using it for my website. Great job guys.

  • whatsonnews

    Great job buddy. Simple pie is just awesome! I find yahoo pipes a bit confusing though.

  • Pingback: Merging RSS Feeds With SimplePie | Webmaster-Source – auguag

  • Kazeljap

    Hi!chvi! uyyum cdqpz yglig ijpze

  • Caleb

    how would i be able to to filter the feeds based on year? (eg. only show the feeds from 2007)

  • Gianfranco

    Is it possible to setup SimplePie to have a website displaying multiples feeds, but :

    when you click on a feed "item" (permalink), instead of going to the item's full article on the feed's website, you dispaly the article on your own website, with your style (and of course you provide a link to the original source / item's article on feed's website).

    Kin dof, instead of that:
    list of feeds > list of items > original article on feed website.

    list of feeds > list of items > content > (link to original article on feed website).

    Thanks in advance for enlighting me.

  • redwall_hp

    It's *possible*. You could change the link that would go to the source to[permalink_URL] then have that page parse the feed again and display the content that matches the permalink. Or something like that…

    Note that you must display summaries of content only. Displaying the full content on your site would be copyright infringement, and illegal in most countries. (And Google also will penalize sites that have the same content as another.)

  • Gianfranco

    Thanks for the answear, redall_hp.
    Of course it should display summary and not the whole content. Thing is, I am supposed to build something for a client and I wanted to make sure that if they need something like that, I don't go: oh, I don't actually know, I'll check it out.

    Anyway, I will test your code later this week to see if and how it does the job.

    Mmm, should I just replace:
    <a href="<?php echo $item->get_permalink(); ?>">


    <a href=";&gt;

    I don't think I get it right…

    Thanks a lot…

  • redwall_hp

    That would be the start. Then you would have to create a page called display_permalink.php that would have some SimplePie logic in it. You're probably better off asking on the SimplePie support forum for the specifics… They're the real experts. :)

  • jason cairns

    hi, I want to change the url link in the title of my rss feed. See Each feed has a title that goes to that twitter id, but I'm going to use yahoo pipes to remove the twitter username from the rss feed, when I do that, the url title nolonger goes to the twitter name, but to a yahoo pipes website. I'm so lost on how to fix this, there must be some easy way to change the link of the rss title of the feed in simple pie! Please Help

  • Pingback: 14 Tutorials to Get You into SimplePie - The Designed

  • Pingback: benjamin junior » Blog Archive » Bookmarks em August 12th

  • Ryan.J

    this post has send a years, but very useful, i find out the A Collection of Beginner Simplepie Tutorials, maybe can helpful somebody like me for a Beginner

  • Designs Edge

    I am testing your script, and love it thus far. I would like the ability to show more than 10 items, I have changed the variable to:
    php foreach($feed->get_items() as $item):
    if ($itemlimit==20) { break; }

    Any idea why I am still getting 10? I have adjusted the cache value as well.

    • Matt

      First, make sure that the feeds have more than ten items altogether. :)

      Also, make sure you delete the files in the cache.

  • ken

    Thank you so much for this guide. I really appreciate you posting it.
    I would like to also add these two items to each of the feed: a category and image (link) unique to each single feed. I will have the information stored somewhere and I just want the values to be assigned to each feed line as the code is looping through the feeds.
    Does anyone know how I can do that? I will really appreciate it.

    • Matt

      You could create a multidimensional array containing the feed URLs and your other data, which you could probably match up with the items somehow?

      • ken

        how do you do that.
        I have researched and I can’t seem to find a way to.
        Any help will be much appreciated.

  • Mossack Anme

    Why my feed cant load the description and date? See it through my name.

  • Linda Harrison


    I tried to test this out on my server, and it doesn’t seem to be working. Can you give me any suggestions? This is where I did it, I copied everything precisely and only changed where the file was included
    Any help would be greatly appreciated!

    Linda x

  • Bearman

    maybe geshi would help for code on wordpress ;)

    and for validating the generated rss-feeds:


    PS: (it seems there is a broken JS …)

  • David S.

    I did something similar recently, but ended up writing the class myself. It does a lot of the same stuff as SimplePie, but also sorts by pubDate.

    If anyone is interested in seeing what I did, I blogged about it here:

  • Jan

    Hello! Is it possible to add to each RSS item the title of the website or feed?

    I mean, if I have 2 feeds:

    I would like to have the result like this:
    WebmasterSource: Item title nr. 1
    WebmasterSource: Item title nr. 2
    ProBlogger: Item title nr. 3

  • Muskie

    I think what Jan wants to do is totally doable, you need to get items associated feed and attach that title, something like:

    $item->get_feed()->get_title() . ‘:’ . $item->get_title()

    I’m going to try this code out, but what I wanted to know about was thumbnails associated with the feed, some people are big on thumbnails and I wanted to preserve them…

  • Muskie

    I don’t think this is working with the latest version of SimplePie. Even fixing the line about forcing XML to:


    It generates an error about header rewriting:

    Warning: Cannot modify header information – headers already sent by (output started at /home/muskie/domains/ in /home/muskie/domains/ on line 1751

  • Muskie

    I got it fixed, you want to remove the line:

    <?php echo ‘’; ?>

    And move the rest of the first section after you call:


    I also specify XML here and I changed this line too:


  • JL

    This was very helpful. Thank you for posting.
    painters Geelong

  • JL

    This really helped.
    painter Rockhampton