CodeIgniter MailChimp 1.3 API Library

May 15th, 2011 - Posted by Steve Marks to PHP, Web Development.

CodeIgniter MailChimp 1.3 API Library

There are lots of email marketing applications out there nowadays but by far my favourite is MailChimp. Alongside its strong use of AJAX to build an easy-to-use interface and its witty banter displayed throughout, it also has a great API allowing third parties to integrate with MailChimp accounts through simple POST requests.

By using the existing class provided by MailChimp I have now amended it so that it can be easily referenced through the PHP framework CodeIgniter by way of a new library.

Downloading and Installing the Library

You can download the library from the following location:

Download Library

View GitHub Repository

Within the ZIP there is a config file containing your MailChimp API key and the library itself. These should be placed into ‘application/config’ and ‘application/libraries’ respectively.

Your API key can be obtained by logging into your MailChimp account and navigating to ‘API Keys & Info’.

Using the Library

To use the library I recommend that you first get a good understanding of the MailChimp API itself. You can find the documentation for this at the following URL:

View the MailChimp API Documentation

Without going into every single method (that would take days) I have included just a few of the more common scenarios below to show how the MailChimp API and CodeIgniter can be used together:

Outputting lists:

$retval = $this->mcapi->lists();
if ($this->mcapi->errorCode){
	echo "Unable to load lists()!";
	echo "\n\tCode=".$this->mcapi->errorCode;
	echo "\n\tMsg=".$this->mcapi->errorMessage."\n";
	echo "Lists that matched:".$retval['total']."\n";
	echo "Lists returned:".sizeof($retval['data'])."\n";
	foreach ($retval['data'] as $list){
		echo "Id = ".$list['id']." - ".$list['name']."\n";
		echo "Web_id = ".$list['web_id']."\n";
		echo "\tSub = ".$list['stats']['member_count'];
		echo "\tUnsub=".$list['stats']['unsubscribe_count'];
		echo "\tCleaned=".$list['stats']['cleaned_count']."\n";

Subscribing an email address:


$listID = "XXXXX"; // obtained by calling lists();
$emailAddress = "";
$retval = $this->mcapi->listSubscribe($listID, $emailAddress);
if ($this->mcapi->errorCode){
	echo "Unable to subscribe email using listSubscribe()!";
	echo "\n\tCode=".$this->mcapi->errorCode;
	echo "\n\tMsg=".$this->mcapi->errorMessage."\n";
	echo $emailAddress." added successfully\n";

Disclaimer: Please note that I did not write the class to calling the MailChimp API. Credit for this should go to the MailChimp developers. I have simply modified it to work within the CodeIgniter framework.

This entry was posted on Sunday, May 15th, 2011 at 5:03 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (2)
  1. @Gary Bisaga Hi Gary. I would recommend in the short term that you simply rename the library file to be ‘Mcapi.php’. I will certainly look into this further though and if it proves to be a consistent problem I will change the filename. Hope that helps

  2. Gary Bisaga says:

    Good job on this. I do have a question though – it seems to have problems with the library loading. I have a MAMP installation on my Mac that I use for development, and this works:


    But when I move it up to the production machine (on EngineHosting), it likes it capitalized this way:


    I have not found a way that works on both. Is this a consequence of having the all-caps library name? I’ve never seen a CI library that uses all caps like that.