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

Good job! Thank you for sharing this. I am working with simplepie now. Their site is down and I needed some code!
nada
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.
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.
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!
I’m getting posts shuffled. is there a way to force a sort by post time?
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.
It looks like there were incompatible date formats – one atom, one RSS 2.0.
Can you give me the URLS of the feeds? I’ll look into it.
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.
Viewing the source of your merged feed (http://www.planetubuntu.es/rss.php), I don’t see any pubDate tags.
hi… where and how to create smallpie.inc file.. please help me to get that file
You can find it at http://www.simplepie.org.
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 ??
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.
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.incoridna.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
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.
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.
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.
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
@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.
In the text file, “php” doesn’t really appear after the “<?” ?
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.
@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();
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
@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.
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://www.macfreak.nl/readnews.php?action=rss“. All the other feed url’s are working, but that one doesn’t.My feed url is “http://www.aizokroon.nl/widgets/feed.php“.
@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.
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://www.macfreak.nl/readnews.php?action=rss. 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?
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?
Nobody haves an idea?
@Frog. I’m not sure what your problem is exactly. I’d recommend trying the official SimplePie support group http://tech.groups.yahoo.com/g.....e-support/
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.
This is a wonderful tool. I used it on my blog and now working on using it for my website. Great job guys.
Great job buddy. Simple pie is just awesome! I find yahoo pipes a bit confusing though.
Hi!chvi! http://ryntznrs.com uyyum cdqpz http://gweocgdv.com yglig ijpze
how would i be able to to filter the feeds based on year? (eg. only show the feeds from 2007)
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.
That:
list of feeds > list of items > content > (link to original article on feed website).
Thanks in advance for enlighting me.
It's *possible*. You could change the link that would go to the source to yoursite.com/display_permalink.php?q=[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.)
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(); ?>">
with:
<a href="http://www.mysite.com/display_.....t;>
I don't think I get it right…
Thanks a lot…
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 http://tech.groups.yahoo.com/g.....upp… They're the real experts.
hi, I want to change the url link in the title of my rss feed. See http://www.healthhive.com/twitter 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
Have you tried the SimplePie support forum yet?
http://tech.groups.yahoo.com/g.....upp…
They of all people should know how to fix it.
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
http://www.devirtuoso.com/2009.....-of…
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.
First, make sure that the feeds have more than ten items altogether.
Also, make sure you delete the files in the cache.
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.
Jack.
You could create a multidimensional array containing the feed URLs and your other data, which you could probably match up with the items somehow?
how do you do that.
I have researched and I can’t seem to find a way to.
Any help will be much appreciated.
Why my feed cant load the description and date? See it through my name.
Hi,
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 SimplePie.inc file was included http://lindaharrisonmusic.com/rss_all.php
Any help would be greatly appreciated!
Linda x
Copy and paste from this file: http://www.webmaster-source.co.....gerphp.txt
WordPress messed up the quotes. (It converted them from straight quotes to the fancy curly ones.)
maybe geshi would help for code on wordpress
… http://wordpress.org/extend/plugins/scgeshi/
and for validating the generated rss-feeds: http://www.feedvalidator.org
greetz
Bearman
PS: (it seems there is a broken JS …)
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:
http://www.widgetsandburritos......php-cache/
Hello! Is it possible to add to each RSS item the title of the website or feed?
I mean, if I have 2 feeds:
http://feeds.feedburner.com/Webmaster-source
http://feeds.feedburner.com/Pr.....sEarnMoney
I would like to have the result like this:
WebmasterSource: Item title nr. 1
WebmasterSource: Item title nr. 2
ProBlogger: Item title nr. 3
…