How to load and use SimplePie 1.3+ with CodeIgniter 2.1+

Posted by Danny Herran on Jun 18, 2013 in Backend | 3 comments

SimplePie RSS CodeIgniter

The famous RSS parser, SimplePie, has changed a few rules in the last couple of versions. The previous methods used to install the library in CodeIgniter are now obsolete. Lets take a look at the new method to get it working in just 5 minutes.

1. Download SimplePie: http://simplepie.org/downloads/

2. Inside ./application/third_party/ create a new folder named “SimplePie”.

3. Extract your SimplePie zip, and copy the library/ folder and autoloader.php to the “SimplePie” folder you just created in the previous step.

4. Inside ./application/third_party/SimplePie create a new folder named “cache”. The following image shows how your “SimplePie” folder should look:

simplepie-codeigniter-structure

5. Place the following code inside a file named “RSS.php” and save it to ./application/libraries/

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH.&quot;/third_party/SimplePie/autoloader.php&quot;;

define('SIMPLEPIE_CACHE_PATH', APPPATH . '/third_party/SimplePie/cache');

class RSS extends SimplePie
{ 
	public $cache_location = SIMPLEPIE_CACHE_PATH;

    public function __construct() { 
        parent::__construct();
    } 
}

6. To use SimplePie in your controller, just load the RSS library as you normally would load any other library. Take a look at the example below for further reference:

$this->load->library('rss');

$feed = $this->rss;
$feed->set_feed_url('http://your.domain.rss.feed/feed/');
$success = $feed->init();
$feed->handle_content_type();

$this->load->view('home', array('feed' => $feed));

The previous instructions were tested on CodeIgniter 2.1.3 and SimplePie 1.3.1.

If you have anything to add, please feel free to do so in the comments sections below.