Easy RSS Consumption with Simple Pie in PHP
Introduction
RSS Feed consumption has always been a stick point in PHP. Simple Pie is a library that works with both PHP4 and PHP5 that enables you to easily parse and save RSS feeds. Today I’ll show you some common uses of Simple Pie and explain how it runs.
Simple Pie requires:
As well as a few other PHP modules that will be enabled in a vanilla PHP install. To run the compatibility test, upload the un-compressed archive to your server and navigate to www.yourname.com/pathtosimplepie/compatibility_test/sp_compatibility_test.php .
Finding the RSS Feed
The first thing we need to do is include SimplePie and then make a new SimplePie feed.
include('path/to/simplepie.inc');
$feed = new SimplePie('lastkarrde.com');
Simple Pie has a very useful ‘auto feed discovery’ feature. If you give it a plain URL, such as Query7.com, it will automatically find the RSS feed. Of course you can specify the path to the .xml file or Feed Burner page if you wanted to. Although the feed has been pulled we need to do call on another function, handle_content_type, to ensure that the feed has the correct mime type and encoding.
$feed->handle_content_type();
Parsing the RSS Feed
Now that all of the configuration/setup is done (all 3 lines of it) we can parse the feed on our page. We use a foreach loop to iterate through all of the items. Simple Pie’s functions make everything easy! As you can see below we simply echo the date, content and title of each item in the RSS feed.
foreach($feed->get_items() as $item){
echo '<h3><a href="' . $item->get_permalink() . '">' . $item->get_title() . '</a></h3>';
echo '<p>' . $item->get_date() . '</p>';
echo '<p>' . $item->get_content() . '</p>';
}
Extra Data from the Feed
In the above example we only got the permalink, title, date and content from each feed item. Simple Pie provides alot of other functions that are used in the same way. Some of the more useful ones are.
- get_author(s) – Get the author(s) of the post item (if set).
- get_description – Gets a summary of the post content, rather than the whole post like get_content() does.
- get_source - Returns the source site of the feed. This is particularly useful if you are parsing multiple feeds and displaying them on one page.
- get_title – Returns the title of the entire RSS feed, wouldn’t be called in the loop.
Parsing Multiple Feeds onto one Page
With Simple Pie, this is painfully easy. When declaring a new instance of the Simple Pie class, we pass an array of websites rather than a string. Instead of..
$feed = new SimplePie('lastkarrde.com');
We would use
$sites = array('query7.com', 'digg.com', slashdot.org');
$feed = new SimplePie($sites);
Caching
Simple Pie comes with an easy to use and very effective caching system. By default it caches each feed for 60 minutes, then it automatically discards it and pulls a new copy of the feed. This is great for a ‘typical user’ who just wants to use Simple Pie for a few things but for someone who knows the feed they are monitoring updates every 2 hours.. or every 20 minutes you need to change the length of caching time. This is done with the set_cache_duration() function. You specify it after you have pulled the feed, but before you display any information onto the page. You can also change the location of where the feed is cached with the set_cache_location() function. Both are demonstrated below.
$feed = new SimplePie('query7.com');
$feed->set_cache_duration(1800); //Value in seconds, 1800s = 30 minutes
$feed->set_cache_duration('/home/user/simplepie/customcache'); //Full path to cache
//Continue with display code here.
As you can see Simple Pie makes parsing RSS feeds really easy. You don’t have to parse them directly onto the page, you can just as easily put database INSERTs into the loop rather than echos and display them from the database at a later date. Simple Pie is an open source project so you don’t need to worry about any fees or license agreements.
If you have any questions about Simple Pie, feel free to post them below.


