Mailchimp API - Add Subscribers to list in PHP

LinkedIn Tweet Facebook
Mailchimp API - Add Subscribers to list in PHP

This article assumes that you already have a Mailchimp account. The API call in the example is made using PHP.

Create Mailchimp API keys

Go to mailchimp select account, you will find extras dropdown. Select API keys and in the bottom left you will find a button Create A Key. Click on it and your api key is created.

Create mailing list in mailchimp manually

Log in to your mailchimp account, click on the lists tab and create list. Enter the required details and click on save.

Test Mailchimp API in Postman

Create three APIs through Postman

  1. Get lists API
  2. Create lists API
  3. Add member API
https://<dc>.api.mailchimp.com/3.0

Replace <dc> part of the URL to the last part of your MailChimp API key, for example us2

https://us2.api.mailchimp.com/3.0/

Set the authorization header with your api key as follows.

apikey bb68e3XXXXdc65XXXXXX49050708f3a-us2

As you can see in the above image, we just need to do GET request to get the mailing lists.

https://us2.api.mailchimp.com/3.0/lists

To create a list, set the authorization header and content-type to JSON and add body with JSON data

{
    "name": "",
    "contact": {
        "company": "",
        "address1": "",
        "address2": "",
        "city": "",
        "state": "",
        "zip": "",
        "country": "",
        "phone": ""
    },
    "permission_reminder": "",
    "use_archive_bar": "",
    "campaign_defaults": {
        "from_name": "",
        "from_email": "",
        "subject": "",
        "language": ""
    },
    "notify_on_subscribe": "",
    "notify_on_unsubscribe": "",
    "email_type_option": "",
    "visibility": "",
    "double_optin": ""
}

To add a member, set the authorization header and content-type to JSON. and add body with JSON data

{"email_address":"abc@gmail.com", "status":"subscribed",  "merge_fields": {
    "NAME": "John Doe",
    "PHONE": "97******20"
  }}

PHP: Get the users from the form

Create a form with the required details for a user. Process the request in php and use the below code to add subscribers to the mailing list.

// API to mailchimp ########################################################

$authToken = 'bb68e3XXXXdc65XXXXXX49050708f3a-us2';
// The data to send to the API
$postData = array(
    "email_address" => "$email", 
    "status" => "subscribed", 
    "merge_fields" => array(
    "NAME"=> "$first_name $last_name",
    "PHONE"=> "$phone")
);

// Setup cURL
$ch = curl_init('https://us2.api.mailchimp.com/3.0/lists/'.$list_id.'/members/');
curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => array(
        'Authorization: apikey '.$authToken,
        'Content-Type: application/json'
    ),
    CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
// #######################################################################

You can fetch the $list_id from the listing api https://us2.api.mailchimp.com/3.0/lists