Elliot Haughin

I'm a freelance web developer.
I build clever, engaging, ass-kicking,
web applications. You want me?

SimplePieParsing RSS feeds with PHP has always been a bit of a pain the ass.
But, SimplePie makes it much simpler!

In your controllers, simply load the library, set your feed url.. and you’re away!

$this->load->library('simplepie');
$this->simplepie->set_feed_url('http://feeds.haughin.com/haughin');
$this->simplepie->set_cache_location(APPPATH.'cache/rss');
$this->simplepie->init();
$this->simplepie->handle_content_type();
 
$data['rss_items'] = $this->simplepie->get_items();

Then, to use the feed data:

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

You can pretty much use the data how you like… check out the simplepie documentation to see all of the data you can get from a feed.

Download Simplepie CodeIgniter Library

Version: 1.0

Sponsored Links

Comments

  1. [...] Elliot Haughin | SimplePie CodeIgniter Library Code Igniter com XML (tags: codeigniter library php rss simplepie) [...]

  2. [...] Elliot Haughin | SimplePie CodeIgniter Library (tags: codeigniter simplepie rss) [...]

  3. [...] within the already powerful CI environment. If interested, head over to Elliots website and download the code. Posted in CodeIgniter at July 1st, 2008. Trackback URI: [...]

  4. Jefferson Rafael says:

    very very good…..

  5. Mark Perkins says:

    Elliot, thanks for the port, I use it all the time.

    Just a minor point – in the second part of the code example (’Then, to use the feed data…’) I think the first and last lines of code should have ‘ul’ instead of ‘li’ in there.

    Thanks again for all your work on CI libs!

  6. Khoa says:

    I know SimplePie, and i had test code. Currently I only get the title and description, I’d like to get all of the contents show up.Please help me!.

  7. Benoa says:

    Hi Elliot,

    Thanks for the great classes you provide around here. That’s a real plus for the CI community.

    Here I don’t get what code did you add to SimplePie, as I thought I’d be able to use it “as is”? Could you explain to me please?

    Good luck with BinaryCake =)

  8. Patrick Hamann says:

    Hi elliot,
    Firstly thank you for porting simplepie into a CI library.

    One question: Is it possible to have two instances of the simplepie object within a controller?

    I.e. normally i would just do:

    $blog = new SimplePie();
    $delicious = new SimplePie();

    Then set each instances feedurl and attributes separately, and display them separately on the page..

    Cheers

  9. Hetal says:

    How different is this port from the actual class? I have used the actual Simplepie class with CI easily. Thanks.

  10. Matt says:

    @Patrick,

    Yes, it is possible. I used a function to do so, and I left it in my default controller instead of a model, though it could work either way. The function calls the example given above. Granted it does not error or exception checking, but it does the trick for now:

    function _runSimplepie($feed){
    $this->load->library(’simplepie’);
    $this->simplepie->set_feed_url($feed);
    $this->simplepie->set_cache_location(APPPATH.’cache/rss’);
    $this->simplepie->init();
    $this->simplepie->handle_content_type();

    $feed = $this->simplepie->get_items();
    return $feed;
    }

    Then I added the following values to the $data array in my controller and passed them to my view:

    $data['haughlin'] = $this->_runSimplepie(’http://feeds.haughin.com/haughin’);
    $data['nettuts'] = $this->_runSimplepie(’http://feedproxy.google.com/nettuts’);

    Using the code Elliot gave as example, they both came in just fine.

    Hope this helps!

  11. George says:

    How can you limit the number of feeds output by Simplepie?

  12. I couldn’t get this to work with Twitter feeds. Any idea what might be going on? I tried the fix the SimplePie folks recommended, but it didn’t help. Thanks.

  13. Nevermind, I was using the wrong feed. I tried taking the one from my home page (twitter.com/) but I needed to use the one from my profile page (twitter.com/my_user_id) Thanks!

  14. Aljaz says:

    Hey,
    Everytime I try to include Simplepie in my CI application I get no response from apache server (no error or anything).

    I also tried including it without CI load library function (include ’simplepie.php’) but it will cause the same thing.

    Anyone has any idea what might be going on?
    I checked the php max buffer configuration and its set to 126mbs so there shouldnt be any problem.
    Heres the apache log for the event:
    [Thu Sep 17 17:21:32 2009] [notice] Parent: Created child process 2804
    [Thu Sep 17 17:21:33 2009] [notice] Child 2804: Child process is running
    [Thu Sep 17 17:21:33 2009] [notice] Child 2804: Acquired the start mutex.
    [Thu Sep 17 17:21:33 2009] [notice] Child 2804: Starting 64 worker threads.
    [Thu Sep 17 17:21:33 2009] [notice] Child 2804: Starting thread to listen on port 80.

    Thanks in advance!

  15. joe says:

    hey,

    nice work with the simplepie thing and thank you. I have a question, I am using CI 1.7.2. and following what you have done, I got simplepie to work but if I try to use the get_permalink() function, i get an error saying:
    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: url

    Filename: libraries/Simplepie.php

    Line Number: 5481
    and repeats for different things like type and length. Not sure what is wrong!

    God bless

  16. George says:

    I’m also getting errors from the SimplePie class.

    SimplePie 1.1 is not compatible with PHP 5.3.0 but 1.2 is. I ran the compatibility test suite that comes with the package and everything passed.

    I tried replacing the 1.1 class with the 1.2 release class but get the same issue.

    It looks like an Apache issue. I get a series of errors like this:

    child pid 3871 exit signal Segmentation fault (11)

    Elliot – did you do anything to make the class CI compatible or was it just a copy and paste? Cheers.

  17. Jeffrey says:

    I’ve also confirmed that this isn’t working on CodeIgniter 1.7.2 on PHP 5.3 (on OS X), and using the latest version of SimplePie (currently 1.2) doesn’t work either.

    I was, however, able to get SimplePie 1.2 working as a plain (non-CodeIgniter) PHP include on this machine.

    Elliot, if you could give some direction as to what you did to CodeIgniter-ify Simplepie, I’d be happy to apply that to Simplepie 1.2 to see if that would fix this.

  18. robm says:

    I’m also getting errors from the SimplePie class, but only within Codeigniter. Simplepie test works great.

    My configuration: PHP 5.3, OS X, CI trunk ver, SimplePie 1.2

    Blank page and Segmentation fault (11) error in apache log.

    Any idea?

  19. Spaboy says:

    It crashes apache on Windows…

    Working on Linux Server

  20. Zoran says:

    It keeps crashing the apache on Windows and i used the SimplePie from their website, but the result is same.

  21. Zoran says:

    I just downloaded the newest version from SimplePie and all i did is renaming the library to Simplepie.php from Simplepie.inc, but it will only work if you have PHP version 5.2.2 and bellow. I don’t see what other changes you have made to this library except the renaming the file? I think CodeIgniter doesn’t need any hook to work with Simplepie

  22. Doug says:

    I’ve tried a few different versions of Simplepie so far, with PHP 5.2.9 and CodeIgniter 1.7.2. No luck. Apache segfaults (11) each time with the combination. Separately, with the exact same code, Simplepie performs just fine. I hope someone figures it out. I tried a few different things recommended from different links, like forcing fsockopen instead of curl, and turning off the cache. Neither of these fixed the issue.

  23. Eric says:

    Just add
    error_reporting(E_ALL & ~E_DEPRECATED);
    on top of the main “index.php” file in code igniter.

    It appends because of PHP 5 sending out deprecated error
    when using ” =& new” .

    Works fine now

  24. Suhail says:

    Great man, its just plug nd play…. save my life.
    its so easy and really parse fast. Intially i thought it is available nativly in CI….but nopz and someone has done a great job and ease the life of others.

Leave a Comment

© Copyright 2010 Elliot Haughin