7 August 2013

This tutorial shows how to parse RSS feeds with PHP. From the libraries and methods that I have tested and used SimplePie is by far the best PHP RSS parser. It supports most common RSS modules out of the box.

Get SimplePie library

You can download it from SimplePie download page. Set it up to a folder that you prefer.

PHP RSS parser script

This example with comments should explain how to use SimplePie easily enough.
Source code viewer
  1. // Load SimplePie library from the same directory.
  2. require_once(dirname(__FILE__) . '/simplepie_1.3.1.php');
  3.  
  4. // Initialize SimplePie Class.
  5. $simplepie = new SimplePie();
  6.  
  7. // Give feed url to SimplePie.
  8. $simplepie->set_feed_url('http://browse-tutorials.com/rss.xml');
  9.  
  10. // By default caching feeds is enabled, SimplePie caches the results to cache directory.
  11. $simplepie->enable_cache(false);
  12.  
  13. // You can also strip html tags from the feed.
  14. $simplepie->strip_htmltags();
  15.  
  16. // Init initializes the feed download and parsing.
  17. $simplepie->init();
  18.  
  19. // Return right headers for rendering of the page.
  20. $simplepie->handle_content_type();
  21.  
  22. foreach ($simplepie->get_items() as $item) {
  23. echo '<div class="items-from-feed">'.
  24. '<span class="date">'.$item->get_date('d/m/Y - H:i').'</span>'.
  25. '<a href="'.$item->get_permalink().'"><strong>'.$item->get_title().'</strong></a><br /><br />'.
  26. $item->get_description().
  27. '</div>';
  28. }
Programming Language: PHP