CodeIgniter and SimplePie

CodeIgniter - 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.

  1. SimplePie’s Wiki

6 comments

1 Ryan Parman { 11.14.07 at 10:38 pm }

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?

2 suzkaw { 11.15.07 at 9:35 am }

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.

3 Ryan Parman { 11.15.07 at 10:31 am }

There are a couple of tutorials here: http://simplepie.org/wiki/plugins/start#codeigniter

4 suzkaw { 11.15.07 at 10:38 am }

Thanks for that link Ryan. I wasn’t aware of those tutorials. ;)

5 SimplePie: PHP Plugin For RSS / Atom Feeds | David Bisset: Web Designer, Coder, Wordpress Guru { 11.25.07 at 3:42 am }

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

6 links for 2008-01-17 « Bijay Rungta’s Weblog { 01.16.08 at 8:40 pm }

[...] CodeIgniter and SimplePie — 68KB (tags: feed php rss tutorial codeigniter simplepie integration php_library) [...]

Leave a Comment