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:
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:
$this->load->library('mcapi');
$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";
}else{
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:
$this->load->library('mcapi');
$listID = "XXXXX"; // obtained by calling lists();
$emailAddress = "email@address.com";
$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";
}else{
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.
Related Posts
- CodeIgniter Rightmove V3 .blm Data Feed Parser Library- CodeIgniter Labels Library
- CodeIgniter Google Maps V3 API Library
- Google Maps API Library for CodeIgniter Framework
- Losing CodeIgniter Sessions
Hi, I'm Steve Marks, the voice (or at least the typist) behind this blog. I'm 24 and currently live in the UK.
@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
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:
$this->load->library(‘mcapi’);
But when I move it up to the production machine (on EngineHosting), it likes it capitalized this way:
$this->load->library(‘MCAPI’);
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.