Continuing on my previous two CodeIgniter Posts, CodeIgniter: Interacting With a Database and CodeIgniter: PHP Development Fun – Part 1, I wanted to spend some time today talking about SimplePie and its integration into CodeIgniter. I still haven’t gotten my head around the Model part of the MVC system, so some of this might be incorrect, but it has worked for me thanks to how forgiving CodeIgniter is.
What is SimplePie
SimplePie is an easy to use class that allows you to interact with RSS feeds in a very simple manner. It is free and is used on many great projects. It will allow you to easily grab, parse and display content from RSS feeds.
Integration Instructions
- Download simplepie.inc
- Rename simplepie.inc to simplepie.php and save it to system/application/libraries folder.
- load the library somewhere in your controller: $this->load->library(’simplepie’);
- Reference SimplePie functions using $this->simplepie->function you want to use
My Notes
So what I have been slowly working on over the last few days is TenTopBlogs, my own limited version of something similar to AllTop.
One interesting thing that I wanted to do was have SimplePie automatically find the RSS feed, and use that to pull the last five items every time the page was loaded or the cached page expired.
At first, this seemed like a smart idea as any time anyone would change their feed location, SimplePie would find the new one, but an issue cropped up: some feeds didn’t work. Right now, Google is moving around feeds on Feedburner and SimplePie tried to pull information, but since the feed was just redirecting to another place, it returned nothing, and on my page, I had a blank box. This occurred more than a few times, also on sites with multiple feeds, it sometimes grabbed the first one, which in some cases wasn’t helpful.
I ended up having my application grab the RSS feed URL and store it in the database when I submitted a site in the back end. This allows me to change the feed URL and take control over my application. The downside being that the application isn’t able to react to feed URL changes on its own, but the upside is that it puts me able to easily fix any errors that crop up.
This is my Submit function that is used when I add a new site from my Administration panel.
[php]
function submit()
{
$name = $this->input->post(‘name’);
$siteurl = $this->input->post(‘url’);
$niche = $this->input->post(‘niche’);
$password = $this->input->post(‘password’);
$this->load->library(‘simplepie’);
$this->simplepie->cache_location = BASEPATH .’cache’;
$this->simplepie->set_feed_url($siteurl);
$this->simplepie->init();
$feedurl = $this->simplepie->subscribe_url();
$data = array(
‘name’ => $name,
‘url’ => $siteurl,
‘niche’ => $niche,
‘feedurl’ => $feedurl
);
if ($password == “4ll#yb4s3”)
{
$this->db->insert(‘sites’, $data);
redirect(‘front/add_blog’);
} else {
echo “Sorry, wrong password”;
}
[/php]
What you will see is that I go to the site and then I grab the RSS subscription location once there, all while using SimplePie’s built-in features. I didn’t have to code an extensive program that curled data from the target site, pulling the required data from the xhtml code.
It was very easy to have SimplePie discover the RSS feed for me. I must make note though that this didn’t always work. Some errors did crop up, but because I put the feed location in my database, I was able to manually correct them. If you were to deal with hundreds of feed locations, this could get annoying or tedious, so some error checking in the code would probably be beneficial.