Integrating CodeIgniter and MagpieRSS

So you want to use MagpieRSS from within CodeIgniter, so that you can fetch some feeds and do naughty stuff with them. No problemo. You download Magpie and you find that there are a few required files that depend on each other, and have an extension of .inc instead of the more common .php. Renaming the files will be a hassle, since you must find/replace all instances within the source code, and that will be a nuisance in case you later need to update your version of Magpie.

Turning Magpie into a Helper

Lets see how we can turn MagpieRSS into a Helper within CodeIgniter, which is actually a very easy thing to do.

Assuming you have already downloaded and extracted MagpieRSS, you should now have a folder named magpierss-0.72, i.e. magpierss followed by a dash and the version number. For simplicity’s sake, you should rename it to magpierss, and move the folder with all its contents into your Codeigniter installation, into the application/helpers folder.

Next, we need to create a new file named magpie_helper.php also within the application/helpers folder, so that Codeigniter will recognize it as a helper. In this new file enter:

<?php
	define('MAGPIE_INPUT_ENCODING', 'UTF-8');
	define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
	require_once(__DIR__.'/magpierss/rss_fetch.inc');
?>

The important thing to notice here is the require_once line, which includes the main magpie file. The other lines just make sure that magpie handles its input and its output as utf-8 encoded, which is something I personally need, but your needs may differ, so feel free to skip them.

Now you should be all set and ready to use your new helper.

Using the MagpieRSS Helper

You should now have successfully set up your Magpie Helper, so now let’s try to use it, to make sure it works. In your controller now type:

	$this->load->helper('magpie');
	$rss = fetch_rss('http://www.anastis.gr/feed/');
	if($rss)
	{
		echo '<pre>';
		print_r($rss);
		echo '</pre>'
	}

Now, visit the appropriate URL for your controller, and if it works successfully (and there is no reason I can think of that it wouldn’t), you should now see the feed outputted on the page. Notice how the $this->load->helper('magpie'); matches the filename magpie_helper.php that we created earlier, but without the _helper.php suffix. You could easily name it something like magpierss_helper.php and then call it $this->load->helper('magpierss'); respectively.

That’s all to it

I hope you find this information useful, knowing it’s not much, as I’ve just discovered MagpieRSS myself, and still exploring its functionality. Please let me know of any comments or questions you may have.

You may also like

3 Comments

  1. Hi!

    Thanks for your explication, but I have a question relative to those examples…

    When I try it, I receive following the message:

    “Function split() is deprecated”…

Leave a Reply

Your email address will not be published. Required fields are marked *