MailChimp Integration On a Website Using MailChimp API and PHP
https://artisansweb.net/mailchimp-integration-website-using-mailchimp-api-php/
Last updated
https://artisansweb.net/mailchimp-integration-website-using-mailchimp-api-php/
Last updated
SAJID UPDATED ON DECEMBER 24, 2020 8 COMMENTS3.5kShares Share Tweet Share
MailChimp is one of the popular email marketing services which manage the subscribers of your website. Using MailChimp, you can send a newsletter to your subscribers. In the newsletter, you may write about your fresh content, offers, or announcements on your website. Newsletters are useful to bring the audience back to the website.
Another popular email marketing service providers are:
Most of the websites put a subscription form where they collect the user’s email. This email will directly go to the MailChimp as a subscriber.
In this article, we study how to do MailChimp integration using their APIs and PHP cURL. We are going to add an email to MailChimp. In other words, we will see how to add subscribers to MailChimp using their API.
Before proceeding make sure you have cURL enabled on the server. We are interacting with the external URL which is nothing but MailChimp endpoints. It requires cURL to send requests and receive responses.
Having said that, let’s see how one can add a subscriber to their MailChimp lists through API.
You can integrate this MailChimp integration on any PHP powered websites. For example, on websites that are built using WordPress, Laravel, etc.
In order to interact with the MailChimp API, you need to grab the API key and Audience ID. Follow the below steps and get it.
Login to your MailChimp account. Under the user icon, select the Account.
Click on Extra->API keys.
Under Your API keys section, click on Create A Key. Copy your API key which we need in a moment.
Next, get an Audience ID to which you need to add your subscribers. For this, click on the Audience menu and then select the option Settings from the Manage Audience drop-down.
Under the Settings click on the ‘Audience name and defaults’.
At this stage, you are ready with your API key and Audience id. Now, all we need to do is send an HTTP request to the MailChimp API along with the required parameters.
You can find a list of all API calls on their official documentation. For our tutorial, we need their Add a new list member API call.
This API call requires an API key with an email address and status. For status, we can pass either the ‘subscribed’ or ‘pending’ value. If a value is ‘subscribed’, then the email address gets added directly to the MailChimp list. And if a value is ‘pending’, then a confirmation email will be sent to a user. After confirmation, the user becomes a subscriber.
To integrate this API, place the below code in your PHP file. Make sure to replace the placeholders with the actual values.
In the above code, on the successful execution, you will get a 200 as a response code. We also handled the error if it throws for some reason.
Mailchimp also provides the Audience fields which store extra information about users directly inside the MailChimp dashboard. Just in case, if you are looking to add Audience fields then you can do it by adding one more array element in the above POST request. Here, I am adding values for the default Audience fields FNAME and LNAME.
You may want to organize your MailChimp contacts with tags. For assigning tags to the subscriber you need to send a POST request to the MailChimp API. Refer to the below code that assigns tags Blogger and YouTuber to a subscriber.
The user can remove tags by providing an ‘inactive’ value for the status key.
MailChimp API provides an API call for deleting an email address from a list. As per documentation to delete a subscriber, we need to send a DELETE request to the API. This time you have to pass the md5 hash of an email address to the API endpoint.
I hope you understand about MailChimp API integration on a website using PHP. Please share your thoughts and suggestions in the comment below.
Related Articles
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
On the next page, you will find your Audience ID.
123456789101112131415161718192021222324252627282930313233
<?php$email
= 'EMAIL_ADDRESS';$list_id
= 'LIST_ID';$api_key
= 'API_KEY';
$data_center
= substr($api_key,strpos($api_key,'-')+1);
$url
= 'https://'. $data_center
.'.api.mailchimp.com/3.0/lists/'. $list_id
.'/members';
$json
= json_encode([ 'email_address'
=> $email, 'status'
=> 'subscribed', //pass 'subscribed' or 'pending']);
try
{ $ch
= curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:'
. $api_key); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); $result
= curl_exec($ch); $status_code
= curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
if
(200 == $status_code) { echo
"The user added successfully to the MailChimp."; }} catch(Exception $e) { echo
$e->getMessage();}
1234567891011
<?php......$json
= json_encode([ 'email_address'
=> $email, 'status'
=> 'subscribed', //pass 'subscribed' or 'pending' 'merge_fields'
=> [ 'FNAME'
=> 'ENTER_FIRST_NAME', 'LNAME'
=> 'ENTER_LAST_NAME' ]]);
123456789101112131415161718192021222324252627282930313233343536
<?php$email
= 'EMAIL_ADDRESS';$list_id
= 'LIST_ID';$api_key
= 'API_KEY';
$data_center
= substr($api_key,strpos($api_key,'-')+1);$subscriber_hash
= md5(strtolower($email));
$url
= 'https://'. $data_center
.'.api.mailchimp.com/3.0/lists/'. $list_id
.'/members/'. $subscriber_hash
.'/tags';
$json
= json_encode([ 'tags'
=> [ ['name'
=> 'Blogger', 'status'
=> 'active'], ['name'
=> 'YouTuber', 'status'
=> 'active'], ]]);
try
{ $ch
= curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:'
. $api_key); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); $result
= curl_exec($ch); $status_code
= curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
if
(204 == $status_code) { echo
"The tags added successfully to the User."; }} catch(Exception $e) { echo
$e->getMessage();}
12345678910111213141516171819202122
<?php$email
= 'EMAIL_ADDRESS';$list_id
= 'LIST_ID';$api_key
= 'API_KEY';
$data_center
= substr($api_key,strpos($api_key,'-')+1);
$url
= 'https://'. $data_center
.'.api.mailchimp.com/3.0/lists/'. $list_id
.'/members/'. md5(strtolower($email));
try
{ $ch
= curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:'
. $api_key); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result
= curl_exec($ch); $status_code
= curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);} catch(Exception $e) { echo
$e->getMessage();}