How to Parse RSS Using SimplePie

SimplePie is a very fast and easy-to-use feed parser, written in PHP, that puts the ‘simple’ back into really simple syndication. Flexible enough to suit beginners and veterans alike.

In this tutorial we will find how to parse rss items using simplepie lib

first of all, download the latest release of simplepie (use the single-file version). http://simplepie.org/downloads/simplepie_1.3.1.compiled.php

let’s start,

include the simplepie file in the start of your php file.

define the simplepie class



$feed = new SimplePie();

then set the simplepie options













/* define the user agent to send to the browser, 
default is SIMPLEPIE_USERAGENT */
$feed->set_useragent();
// define the rss link to parse
$feed->set_feed_url($rss_url); 
// This is what makes everything happen.
$feed->init(); 
/* This method ensures that the SimplePie-enabled page 
is being served with the correct mime-type and character 
encoding HTTP headers. */
$feed->handle_content_type(); 

after we prepare the simplepie class, now we can fetch the rss information.






// rss information
$feed->get_title(); // rss feed title
$feed->get_permalink(); // rss feed url
$feed->get_description(); // rss feed description

parsing the rss items and assign them to an array







/* the first parameter is the starter item 
and the second is the length (number) of parsed items.  
$feed->get_items(3,10) means parse 10 items 
starting from the third item. */
$items = $feed->get_items(0,10); 

now we have an array with the rss items. lets fetch it.












foreach ($items AS $item) {
// the item link to the post
$permalink = $item->get_permalink(); 
// the item (post) title
$title = $item->get_title(); 
// the item (post) description; you can also use $item->get_content();
$description = $item->get_description(); 
// the item updated date and time; you can use your date and time formula.
$datetime = $item->get_date('j F Y | g:i a'); 
}

finally, I simplified the way to parse the rss items as much as possible but there are many and many things to do with simplepie you can find it at the official documentation : http://simplepie.org/wiki/.

Leave a Comment

Your email address will not be published. Required fields are marked *