How to integrate your website with Mailchimp API using PHP
MailChimp is one of the most popular email marketing services available on the web. There are several other services like Constant Contact and AWeber, but MailChimp provides 2000 subscribers for 0$ which is perfect for startup projects.
Few months ago my task was to integrate a small website with MailChimp’s API using PHP. Before I start with implementationonal details, I want to share with you the following facts:
- You can’t add a subscriber to the MailChimp list using API, if user didn’t confirmed his/her subscription to the list, meaning you can’t add bunch of random emails to your list
- MailChimp API works with HTTPS but that doesn’t mean you have to install certificate on your website
I will separate integration with MailChimp into three steps installation, implementation and testing.
Here are also some useful links:
- You can find all available MailChimp’s API libraries on this link.
- You can download MailChimp’s PHP library on this link.
- You can see all available API methods here.
- Download the library from the following link.
- In the .rar file find the folder src, and extract its content to your website’s folder (in my case it is C:\wamp\www\MyTestProject).
- Import Mailchimp.php into your project using
- Now you can create new Mailchimp object using the following command:
$mailChimp = new Mailchimp("YOUR API KEY");
- In your MailChimp account create new API key, by opening your account profile page, then clicking Extras->API keys.
- Here is example of the API key:
$mailChimp = new Mailchimp("1891634555wee80aceaakeld1ab707c-us3");
In the implementation I will show you how to add user to a subscription list in the MailChimp. Here is definition of the “lists/subscribe” method:
lists/subscribe (string apikey, string id, struct email, struct merge_vars, string email_type, bool double_optin, bool update_existing, bool replace_interests, bool send_welcome)
You can read more detailed explanation of the method on the following link.
As you can see from mathod definition, we have to provide the following fields:
|Field name||Field explanation|
|id||ID of the list available in the MailChimp|
|update_existing||weather or not to update existing record|
|send_welcome||Send welcome email to customer|
As you can see, the Mailchimp object has one method called
call. The first parameter is the MailChimp’s API method name like “
lists/subscribe“, and the second parameter is PHP array with
As first parameter do not pass full url like: https://us2.api.mailchimp.com/2.0/lists/subscribe, because the first part of the link is already generated in
If everything runs without error, method call will return you some values. Here is example value:
'email' => string 'firstname.lastname@example.org' (length=20)
'euid' => string 'dd569856bc' (length=10)
'leid' => string '154189313' (length=9)
These values can be stored in your database, and uniquely identifies customer in the MailChimp.
If you during runtime encounter the following error:
API call to: lists/subscribe.json failed: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
then you have to perform the following edits in
.pemfile from this location http://curl.haxx.se/ca/cacert.pem
- Copy this
.pemfile in the folder where
Mailchimp.phpfile is located
Mailchimp.phpfile, and add the following code after line 201:
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
http://code-epicenter.com/how-to-integrate-your-website-with-mailchimp-api-using-php/How to integrate your website with Mailchimp API using PHPhttp://code-epicenter.com/wp-content/uploads/2015/03/mailchimp-logo.pnghttp://code-epicenter.com/wp-content/uploads/2015/03/mailchimp-logo-150x150.pngLibrariesPHPProgrammingAPI,MailChimp,PHPMailChimp is one of the most popular email marketing services available on the web. There are several other services like Constant Contact and AWeber, but MailChimp provides 2000 subscribers for 0$ which is perfect for startup projects. Few months ago my task was to integrate a small website with MailChimp's...Amir DuranAmir Duranamir.email@example.comAdministratorAmir Duran is software engineer who currently lives and works in Germany. He obtained Masters degree diploma on Faculty of Electrical Engineering in Sarajevo, department Computer science. With good educational background he is specialized in designing and implementing a full-stack web based applications.Code Epicenter