New here? Then you may want to subscribe to my rss feed. :)

SimplePie RSS class and CodeIgniter

June 18th, 2007 | Posted in Codeigniter, Development, PHP | 14 Comments

Users of the latest version of Simplepie (Version 1.0.1) have been getting errors when passing in the feed url. The error refers to a missing method “feed_url()”. This is due to a change in Simplepie, to set the feed url you must now use “set_feed_url(your rss url)”.

A pet project of mine which I will unleash upon you all soon required the home page to read in an RSS feed. Since I am a CodeIgniter whore I soon learnt that amongst its many libraries and helpers that there wasn’t an RSS reader.

This was easily solved using an open source PHP RSS class called SimplePie. Simply, I can’t imagine ever needing anything else. Not to mention the ease of getting it to work within CodeIgniter. By placing simplepie.inc in the libraries directory and renaming it to simplepie.php, I could now use its methods to parse and display my RSS feed.

Within your controller:

  1. function index() {
  2.         $this->load->library(’simplepie’);
  3.         //$this->simplepie->feed_url(’http://www.digg.com/rss/indexdig.xml’);
  4.         // You may have encountered an error where it couldn’t find feed_url. This is due
  5.         // to a change in the latest version of simplepie. Simply, feed_url is now set_feed_url
  6.         $this->simplepie->set_feed_url(‘http://www.digg.com/rss/indexdig.xml’);
  7.         $this->simplepie->init();
  8.         $data[‘dig_feed’] = $this->simplepie;
  9. }

Passing the $data array holding the parsed feed data to the view template made displaying the data a breeze. get_items() returns all feed data as an array. Optionally, if you wish to only display the first 5 items, get_items takes 2 parameters, the starting position and quantity to return. Passing in 0,5 as arguments returns the first 5 items.

  1. echo "<ol>";
  2. foreach($dig_feed->get_items() as $item) {
  3.         echo "<li><a href=’" .$item->get_link() . "’>" . $item->get_title() . "</a></li>";
  4. }
  5. echo "</ol>";

Any problems then let me know.

14 Responses to “SimplePie RSS class and CodeIgniter”

  • Gravatar
    hamza Location: Egypt //

    One Word : Beautiful , am using simple pie and it’s quite powerful soon as i used codeigniter i integrate it …
    keep up the great work

  • Gravatar
    Fiaz Location: Great Britain (UK) //

    Glad you found it useful.

  • Gravatar
    Hamza Location: Egypt //

    Yes it’s very useful , i often use the Wordpress as a Framework , make the special plugins , and customize , but the code igniter made it more easier , as example year ago i used simple pie with wordpress , for http://www.medpeek.com , with codeigniter it’s only take a mins especially with this tutorial :)

  • Gravatar
    gunnard Location: United States //

    This tut looks perfect but I can’t get simplepie to work, my code is straight copied from you and I have the simplepie.inc renaimed to simplepie.php .. my error is
    “Fatal error: Call to undefined method SimplePie::feed_url() in /Applications/MAMP/htdocs/wootmovie/system/application/controllers/main.php on line 17″
    line 17 looks like ” $this->simplepie->feed_url(’http://www.digg.com/rss/indexdig.xml’);

    whats going on?

  • Gravatar
    Fiaz Location: Great Britain (UK) //

    Thanks for highlighting this. It seems the very latest version has changed resulting in the error you are getting.

    Instead, change feed_url to set_feed_url.

    I have updated the tutorial.

  • Gravatar
    Federico Fasce Location: Italy //

    everything seems working fine, except one thing. I get a cache error:

    A PHP Error was encountered

    Severity: User Warning
    Message: ./cache/f70d607810b0be605b26c22d878878b4.spc is not writeable
    Filename: libraries/simplepie.php
    Line Number: 1623

    But my cache directory (the one installed by codeigniter) is in fact writeable. Should I set up a new cache dir for Simplepie? If so, where?

    Other question: special characters such as accents are not shown correctly. My html uses utf-8 as charset. Is there a way to show the feed results in utf-8 too?

    thank you!

  • Gravatar
    Fiaz Location: Great Britain (UK) //

    I specifically set my cache folder to be the CI cache folder using:

    simplepie->set_cache_location(’full system path to CI cache folder’);

    HTH

  • Gravatar
    Federico Fasce Location: Italy //

    thank you. I’ve got another question, is maybe very stupid, but I’m not much of a coder.
    The code you provided is perfect if you have only one rss feed to display.
    Let’s say I want a popurl like webpage, with some RSS feed displayed in different ways. I think I must have a standard simplepie function to fetch RSS from each different source, and then different views for each different block.

    Now… I’m not sure how to do this and where to put this function. Should I create a library with a sngle Rss fetch function?

    Thank you!

  • Gravatar
    RSS con CodeIgniter Location: United States //

    [...] El artículo original de la adaptación de esta librería, del cual esta entrada es tan solo una traducción muuuuy libre se encuentra en SimplePie RSS class and CodeIgniter. [...]

  • Gravatar
    101 Code Igniter Resources | Learning On Demand Location: United States //

    [...] SimplePie RSS class and CodeIgniter, simplepie and CI work [...]

  • Gravatar
    Code Igniter Framework :: TermiT's Blog Location: United States //

    [...] SimplePie RSS class and CodeIgniter [...]

  • Gravatar
    links for 2008-01-17 « Bijay Rungta’s Weblog Location: United States //

    [...] SimplePie RSS class and CodeIgniter (tags: library parser php rss tutorial simplepie codeigniter integration php_library) [...]

  • Gravatar
    Some CodeIgniter Tutorial links « Afruj’s Weblog Location: United States //

    [...] SimplePie RSS class and CodeIgniter [...]

  • Gravatar
    CodeIgniter Tutorial Links « Brandontruong’s Weblog Location: United States //

    [...] SimplePie RSS class and CodeIgniter [...]

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>