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 might be interested in …

How to check if a shortcode is registered in WordPress

English, PHP, WordPress

Quick and easy function to check if a plugin/theme/whatever has add/registered a shortcode in WordPress: Just add this into your plugin or theme’s functions.php and wherever you need to check if the shortcode exists, just call is_shortcode_defined(“button”);  or something similar from an if statement, as such: Hope this helps.

Read More

Free Open Source Exchange Rates for PHP

English, PHP

Inspired by the Open Source Exchange Rates and money.js, I’ve developed a PHP class that consumes the openexchangerates.org service. Since the service fetches the exchange rates from the (unofficial) Google Calculator API, I played around with it as well, and found some differences on the exchange rates provided by the two services. It is probably […]

Read More

mb_strtok() – A PHP implementation

PHP

While developing a web app, I needed to use the php’s multibyte family of functions. Having to deal with Greek characters specifically (although I always use utf-8) I needed the multibyte equivalent of strtok() to tokenize a stream of Greek characters. A quick look in the php documentation yielded almost every other function, but nothing […]

Read More

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 *