CodeIgniter and SimplePie

I have been trying to get our rss feed into the administration area of 68kb for a few days now and after trying countless ways I just could not get it to work. Finally after reading loads of forum threads I was able to successfully integrate SimplePie. For anyone else having trouble I will go through all the steps I took to get it working.
Step 1.
Download SimplePie. I used v1.0.1
Step 2.
Rename simplepie.inc to Simplepie.php and save it to system/application/libraries folder.
Step 3.
Open your controller file and add this code:
$this->load->library('simplepie');
$link = 'http://68kb.com/category/news/feed';
$data['feed'] = new SimplePie();
$data['feed']->set_feed_url($link);
$data['feed']->enable_cache(false);
$data['feed']->init();
$data['feed']->handle_content_type();Of course make sure you pass “$data” to the view file.
Step 4.
Open your view file and add this:
<?php if ($feed->data): ?>
<?php $items = $feed->get_items(); ?>
<?php foreach ($items as $item): ?>
<strong><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></strong> - <?php echo $item->get_date('j M Y'); ?>
<p><?php echo $item->get_description(); ?></p>
<?php endforeach; ?>
<?php endif; ?>I know this seems pretty basic but it took me ages to get it running. I would like to thank CI user MASS MASS for this forum thread which helped me tremendously.
October 31, 2007 15 Comments


15 comments
Hey Eric, this is Ryan from the SimplePie project. Two things:
1) handle_content_type() will automatically serve HTTP headers, so make sure that you’re not calling this unless it’s being used to serve out your CodeIgniter page.
2) You said it took you ages to get it working. Is there anything that WE can do to make things like this easier for everyone?
Hi Ryan,
Thanks for the comments.
1. I will check into that. I got it working and didn’t look to far into some of the parameters.
2. I am not sure it has really anything to do with SimplePie. For me the confusing part was (1) getting the class called correctly through CodeIgniter, and (2) getting the proper coding for the view file.
Plus I am not sure how many of your users use CodeIgniter which was probably the leading cause of this.
While you are here I also want to say thanks for SimplePie. It is an excellent little script.
There are a couple of tutorials here: http://simplepie.org/wiki/plugins/start#codeigniter
Thanks for that link Ryan. I wasn’t aware of those tutorials.
[...] feeds. Seems to intergrate with CakePHP and CodeIgniter, and there’s even a Wordpress plugin. Here’s a little more on the CodeIgniter intergration. Tags: Atom, CodeIgniter, PHP, RSS, [...]
[...] CodeIgniter and SimplePie — 68KB (tags: feed php rss tutorial codeigniter simplepie integration php_library) [...]
thanks this tutorial is really great you know.
I have added SimplePie to my CI app, an it 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.
this is great!
the way i did multiple feeds was just make a $data['feed'] set your vars then copy all the same code but change the $link var to your different rss and then you change $data['feed'] to $data['feed2'] and if you want 3 you do $data['feed3'] but dont forget to add them to the view. you would just use another foreach and instead of $feed you use $feed2, $feed3 and so on…
thanks for this it worked great!
I integrate SimplePie with CI, with the following snipet of code:
//using siplepie library
$this->CI->load->library('simplepie');
$this->CI->simplepie->set_feed_url($this->endpoint_search . $str_params);
$this->CI->simplepie->set_cache_location(BASEPATH.'cache/');
$this->CI->simplepie->init();
but when i run, i get the following error in a window
“An unhandled win32 exception ocurred in httpd.exe”
any idea about this, thanks in advance,
Hi, I’ve followed your instructions for integrating SimplePie into CodeIgniter and I’ve passed the data to the view file with:
$this->load->view('sidebar', $data);but I get these error messages when I open the page:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: feed
Filename: libraries/Loader.php(673) : eval()’d code
Line Number: 33
and:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/Loader.php(673) : eval()’d code
Line Number: 33
Line #33 is the line in the view file where the PHP for displaying the feed starts. Any idea what might be causing this error?
Thanks!
Can you try disabling short open tags support and see if that fixes it?
Hi Eric,
Thank for replying.
Short open tags were already disabled. I tried enabling them but, of course, it didn’t work. 
Any other ideas why it might not be working?
Thanks!
Honestly I do not know why you are getting that error. I asked about the short open tags because line 673 of Loader.php is:
if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE) { echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path)))); }Which is where it tries to convert to normal php tags.
Well, thanks anyway.
I’ll post a comment if I figure it out. 
Leave a Comment