other posts in CodeIgniter, Development, PHP

SimplePie RSS class and CodeIgniter

June 18th, 2007 | Posted in , ,  | 26 Comments | Posted by Fiaz | | Related Posts

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:

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

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.

echo '<ol>';
foreach($dig_feed->get_items() as $item) {
	echo '<li><a href=' .$item->get_link() . '>'. $item->get_title() . '</a></li>';
}
echo '</ol>';

Any problems then let me know.

  1. Wordtube and protected wordpress posts
  2. Freelancers.Net projects RSS feed
  3. Play a random mp3 file from a specific directory
  4. Extreme Music’s Get Trax Launched
  5. C# and XSLT extension objects


26 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 [...]

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

    hey thanks for the tip a few months ago about codeigniter, Do you know of any good tutorials on creating an rss feed in codeigniter. Haven’t been able to find one that works yet properly.

  • Gravatar
    Vik Rubenfeld Location: United States //

    I have added SimplePie to my CI app using your tutorial, which works great.

    I would like to use multiple rss fees with my CI app, and I have encountered the memory leak anomaly. What is the correct way to apply the fixes for this (link) to a CI installation?

    Thanks very much in advance for any info.

  • Gravatar
    josepzin Location: Spain //

    Thanks! I have use the Simplepie with CakePHP for my RetroPlanet (http://juegario.com/retroplanet), but now a need the same with CodeIgniter.

    For cache setting i have use: $this->simplepie->set_cache_location(BASEPATH.’cache/’);

  • Gravatar
    Блог World Programs » Архив блога » CodeIgniter Framework Location: Russia //

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

  • Gravatar
    Gireesh Location: India //

    awesome work…keep it up

  • Gravatar
    Muhammad Rashid Babsail Location: Indonesia //

    great, its very useful..
    im still wondering, what if i want to fetch an item (row) and save it to the database…
    say like this;

    #1
    #2
    #3
    #n

    and then i want to take item #2….
    IMO first, i have to save all items as an array (parse to HTML/PHP maybe?)
    then take the index…

    any idea?

  • Gravatar
    Steve B Location: United States //

    Hello! Thanks for providing this tutorial! I’m trying to get Simplepie to work in my CI application. My code looks like the following:

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

    $this->simplepie->set_feed_url('http://www.digg.com/rss/indexdig.xml');

    $this->simplepie->init();

    $feed = $this->simplepie;

    $rssitems = $feed->get_items();

    print_r($rssitems);
    die;

    when I try to print the rssitems array nothing is coming up within the array. Am I missing something?

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

    It seems that http://www.digg.com/rss/indexdig.xml redirects to feedburner.
    Try http://feeds.digg.com/digg/upcoming.rss instead.

  • Gravatar
    josh Location: United States //

    Any chance you might be working on an Expression Engine plugin for Simple Pie? I have been trying to get a weather widget I made with some help from the SimplePie docs to work with EE…but no luck?

    I’d be happy to share with you my templates if you have any interest let me know and I can email them to you.

    Josh

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

    Have you tried MagpieRSS?, it comes bundled with EE.
    Documentation

  • Gravatar
    josh Location: United States //

    I have tried MagpieRSS…one of the issues I have with it is the lack (maybe my ignorance?) of conrol when it comes to choosing and styling the displayed results to my needs.

    Maybe you can shed some light on that? Thanks for taking the time to comment.

  • Gravatar
    josepzin Location: Spain //

    Expression Engine is CodeIgniter, if you know CI, may be is not difficult make that widget…

    I use SimplePie and CI for retroinvaders.com

Leave a comment


©2009 Next Big Leap Limited - +44 (0)116 2987671 - CodeIgniter / PHP / Expression Engine Specialists Leicester
Website Design, Website Development, Leicester, Leicestershire