Talking to ExpressionEngine’s MetaWeblog API with CodeIgniter - Christopher Imrie.com

← Next

This site is built on HTML5 & CSS3.

Christopher Imrie is an Adobe Certified Instructor

Talking to ExpressionEngine’s MetaWeblog API with CodeIgniter

On a recent project I had the need to interface between ExpressionEngine (1.6.8) and CodeIgniter.

There are several situations where this is very useful even if you know ExpressionEngine’s module and extension development API’s like the back of your hand.  More often than not it’s just easier to use the Metaweblog API since it ships with the system.

In my case I had a page that included a form with some validation functionality that required, among other things, to have the user confirm via email as well as store some custom data in a database for other uses.  As the rest of the website was being run on ExpressionEngine,  I wanted the users data posted into the ExpressionEngine system once all the validation and custom scripts had been executed so that it could be managed via the control panel.

However well you know ExpressionEngine, inserting directly into the ExpressionEngine database is always a bad idea, since EllisLab may update the database structure and then your custom script could end up wreaking havoc on the database.  So I decided very early on that the Metaweblog API was the way to go.

After looking around on the net, I couldn’t find any good tutorials on how to get CodeIgniter to talk to the Metaweblog API so I figured I’d write my own.

XML -RPC

The Metaweblog API works over what is known as XML -RPC.  Its a pretty common data interchange format that is designed to be simpler than methods such as SOAP.  It works by sending data encoded in a certain XML structure to a server that then processes the data.

If you’re like me, you’ll find reading the official xml-rpc specification to be a little dry and tricky to put into practice in terms of coding a solution from scratch.  Luckily for us, CodeIgniter has a built in XML-RPC server and client library that can get us up and running quickly.

CodeIgniter’s XML-RPC Class

This isn’t a tutorial on how to use CodeIgniter, so if you need a primer on that then check out the official user documentation.  I still believe its the best written user documentation out there.

The CodeIgniter XML-RPC documentation is a great resource on how to use the the client library, however, its not exactly easy to apply it to the Metaweblog API.  After a bit of trial and error I figured it out and although it is very simple I discovered that there are some restrictions to using it.

Setting up ExpressionEngine

Firstly, you’ll have to install the Metaweblog API module in your ExpressionEngine install.  You’ll find this, obviously, under the Modules tab of the control panel.  Once installed you can click on the Metaweblog API to change the settings. Once installed you’ll see something like this:

Make a note of the URL on the configuration page since you will need it later

When using the Metaweblog API  each script or weblog you want to interface with will need its own configuration.  The reason for this is that xml-rpc can only carry so much information, and it is designed for interfacing with simpler blogging systems such as Blogger or Wordpress, where there is one blog made up of the following fields:

If you’re an ExpressionEngine developer though, you’ll know that the beauty of EE is that you don’t need to follow this pattern and you can choose your own. So when using the Metaweblog API, you need to map the incoming excerpt, content, more & keyword fields to your own fields.  You dont need to map them all, you just need to choose what incoming data is going where.

When you click on the name of the configuration you want to edit, you’ll be presented with the following options:

Now although it looks like there are several input data fields you can use, the many here are actually for the MoveableType API which is also part of this module ( though its not really written anywhere).  So for incoming data, you can only use the Content field.

Configuration Name

Enter the name of this configuration. You can leave this as Default, unless you are using more than one interface configuration.

Text Formatting Preference

Choose whether you want ExpressionEngine to parse any EE tags in the incoming data

Entry Status

You can choose what status to apply to the incoming data when it is inserted into the database, or allow it to be specified by the incoming data (more on this later).

Channel Field Group

Select the ExpressionEngine field group you want data inserted into. You must keep in mind that when you are sending data from CodeIgniter, you can specify the weblog to be posted into, however, make sure that you select the corresponding field group for that weblog here.

Excerpt Field

MoveableType API  only - Select what field you want the incoming excerpt data inserted into. The field must be a textarea.

Content Field

Select what field you want the incoming content data inserted into. The field must be a textarea.

More Field

MoveableType API  only - Select what field you want the incoming more data inserted into. The field must be a textarea.

Keywords Field

MoveableType API  only - Select what field you want the incoming keyword data inserted into. The field must be a textarea.

Upload directory for file uploading

If you are uploading files (although not covered here) you can specify where you want those files to be delivered.

So keep in mind that since we are using the Metaweblog API we can only use the Content area for incoming data, and also that the field it gets mapped to must be a textarea.

Setting up CodeIgniter

The code needed in order get the xml-rpc class talking to ExpressionEngine is pretty simple.  All you need to do is load the library in your controller.  Here i am loading the library in the controller constructor.

class Metaweblog extends Controller {

	function Metaweblog()
	{
		parent::Controller();

		//load the library
		$this->load->library('xmlrpc');
	}
}

Once the library is loaded, you need to specify the server address.

class Metaweblog extends Controller {

	function Metaweblog()
	{
		parent::Controller();

		//load the library
		$this->load->library('xmlrpc');

		//specify the server address
		$this->xmlrpc->server($this->config->item('metaweblog_url'));

	}
}

The Metaweblog API has various methods that can be used (eg: Editing, Deleting etc) but in this case we want to post a new item.  So we need to specify the method:

class Metaweblog extends Controller {

	function Metaweblog()
	{
		parent::Controller();

		//load the library
		$this->load->library('xmlrpc');

		//specify the server address
		$this->xmlrpc->server($this->config->item('metaweblog_url'));

		//Choose the metaWeblog API method
		$this->xmlrpc->method('metaWeblog.newPost');
	}
}

So now our configurations are in, we just have to format the xml to the correct structure.  Using the library in CodeIgniter, we set the structure of the resulting xml by creating an array():

//Setup paramaters for posting
$data = array(
	//Param[0]: Weblog ID
	"weblog ID",
	//Param[1]: Username
	"username",
	//Param[2]:Password
	"password",
	//Param[3]:Data
	array(
			array(
				'title' 		=>	"Title of the weblog",
				'description'	=>	"Content of the weblog data being posted"
				//This is needed for the array to be recognized
				), "struct"
		),
	//Param[4]:Status
	1
	);

I’ve commented the code above, so its pretty self explanatory.  As you can see, you specify the title and description data.  Whatever is in the description will be inserted into the field you chose to map the “Content field” to in the ExpressionEngine Metaweblog API module settings.

Once we’ve set this array up, we then add the array to the xml-rpc library and send the request.  So once this is all put together, the full controller looks like so:

class Metaweblog extends Controller {

	function Metaweblog()
	{
		parent::Controller();

		//load the library
		$this->load->library('xmlrpc');

		//specify the server address
		$this->xmlrpc->server($this->config->item('metaweblog_url'));

		//Choose the metaWeblog API method
		$this->xmlrpc->method('metaWeblog.newPost');
	}

 	function publish()
	{
		//Setup paramaters for posting
		$data = array(
			//Param[0]: Weblog ID
			"weblog ID",
			//Param[1]: Username
			"username",
			//Param[2]:Password
			"password",
			//Param[3]:Data
			array(
					array(
						'title' 		=>	"Title of the weblog",
						'description'	=>	"Content of the weblog data being posted"
						//This is needed for the array to be recognized
						), "struct"
				),
			//Param[4]:Status
			1
			);

			//Add the array to the xml-rpc library
			$this->xmlrpc->request($data);

			//Make the request
			$this->xmlrpc->send_request();
}
}

And thats it!

If you are having trouble with the data, and want to know what is going on, you can turn on the debug mode by adding the following line in the contructor of your controller:

$this->xmlrpc->set_debug(TRUE);

Hope this is of help to someone.