How to consume RSS Feeds in PHP using SimplePie

I recently had to consume different feeds and display their contents on a webpage which led me to finding the open source library “Simple-Pie” which was developed by Ryan Parman. Writing from a scratch, codes to consume and parse the feeds amounts to a waste of time (clients are always on your neck to deliver on time), quite tedious (yes, I am lazy) and for Pascal’s sake, why should I reinvent the wheel?

Let’s get started, you can download the zip file of the current working version from the repository at https://github.com/simplepie/simplepie/. Unzip the file to a location in your project folder, and that’s all for installation. To use simple pie, basically, you need PHP 5.2.0 or higher installed, cURL extension enabled. If cURL is disabled, simple pie tries to use fsockopen(), and I’ll assume you know cURL and fsockopen() are used for pulling contents of the webpage.

In the unzipped package, for a quickstart, you just need to keep the library folder which contains the SimplePie classes and autoloader.php file. You can delete the rest if you are concerned about saving some few kilobytes of space.

For our example, we would use www.punchng.com/feed as the URL to our RSS feed, and in case you are wondering, it’s just a Nigerian Newspaper website feed.

Let’s create our main script, say feed-consumer.php, the declare the location of the autoloader.php file in the unzipped simple pie package as shown below:



Include the file, or require, depends on your reason by doing:


include_once($location);

Create a new instance of the Simple pie object:


$feed = new SimplePie();

Declare and assign the URL to a variable:

  $url = ‘http://www.punchng.com/feed’; 

Make sure you strip slashes if magic quotes is enabled, if you are sure it is not, do not bother, here’s how to strip slashes.


if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())

{

$url = stripslashes($url);

}

Then Set the URL property of the earlier instantiated simple pie object:

 $feed->set_feed_url($url); 

Call the init() method of the instantiated simple pie object ($feed) and pass the result into a variable. The init() method is used to read the feed, process it, parse it. Upon successful execution, the method returns a true value and otherwise if failed. Heres how;

 $result = $feed->init(); 

We need to make sure that the right content type and character encoding gets set automatically. This function will grab the proper character encoding, as well as set the content type to text/html.

 $feed->handle_content_type(); 

If any error, do something with the error message, here’s how to check if there is any error and display the errors.

 if ($feed->error()) echo($feed->error()); 

If the parsing was successful, we need to work with the contents of the parsed feeds. Basically, most RSS feeds I have worked with have a generic format, at the top level, we have title of the feed, the link of the feed, and the description of the feed. Each of the feed items also basically consist of link of the item, a title for the item and the actual content of the item. Obviously, the main reason of consuming the feed is to extract all the aforementioned. Here’s how:


If ($result){

if ($feed->get_link()) echo '';

echo $feed->get_title(); if ($feed->get_link()) echo '';

echo $feed->get_description();

foreach($feed->get_items() as $item)

{

echo ($item->get_title().’
’); echo ($item->get_permalink().’
’); echo ($item->get_date('j M Y, g:i a').’
’); echo ($item->get_content().’
’); } }

The steps above would definitely work for users who just want a quick way to use simple pie to parse and extract all the text information from the RSS feed. Drop any comment if any issue arises.

And I just heard on the news that Sepp Blatter wants to go for another term, what a greedy, 78-year-old fellow I’d say he is!