SimplePie: Parse RSS With PHP

LastRSS. MagpieRSS. Neither have been updated in over a year. What’s the best way to parse RSS feeds and output them with PHP? Simple, use SimplePie.

What is SimplePie? It’s a file you include in your PHP scripts, allowing you instant access to RSS-parsing capabilities. You can display RSS headlines/items on your website, combine several feeds into one, and anything else that you can think of. If you know a little PHP, the possibilities are endless.

You see this PHP code:

$feed = new SimplePie("");
echo "<h1>".$feed->get_title()."</h1>";
foreach ($feed->get_items() as $item) {
echo '<h2><a href="'.$item->get_permalink().'">';
echo $item->get_title().'</a></h2>';
echo '<p>'.$item->get_description().'</p>';

That’s all you need to display a feed’s headlines and descriptions. Of course, that was only a simple example. It’s unbelievable what you can do with SimplePie.

Over at NTugo, SimplePie is used extensively. The MyNT RSS reader is powered by SimplePie. The “Recent blog posts” and “Recent posts from the NTugo Network” boxes on the home page are built with SimplePie. The RSS feed aggregated from all of the NTugo blogs? SimplePie again! It’s amazing what you can do with SimplePie.

If you know PHP, you’ll definitely want to take a look at SimplePie.

  • TheDashboard

    …where i can build in the image tag ? Or, how do the Feed display the images ??? Thank You ! BTW: I do not add my mail/www address because of hideous dangerous spam ! – so i never ever post any details to anywheres site again !

  • Will

    Thanks for the great post. I’ve been looking around for a good way to grab RSS from blogs, but most ways require the use of ads or scripts that are no longer maintained.

  • Kuntal

    I was wondering if there was a way to use this so that I can extract the full text from the body of the actual post, but not any of the text that is in advertisements, or the menus on the page. For example on this page:

    I would only want the actual post and not the text that shows up on teh left like recent comments, recent posts, etc…also is there any way to parse out comments individually, so I can extract each comment to a different cell in excel or field in a database?

    thanks for the great work!

  • A.Faith

    let's say the feed i want to parse contains only images (jpg files) and i want to save them in a local directory for later review. how can I do that? i would be more than grateful if you can point me to a tutorial that could help me or you can give me some code examples :)

  • Richard.Williams

    Helpful article. Thanks. Can also use biterscripting ( ) for extracting headlines and descriptions and other things from an RSS. Google up biterscripting and RSS to see some scripts people have posted. There are a lot of them.