You are currently viewing Mailchimp API – Add Newsletter subscribers to mailchimp using PHP

Mailchimp API – Add Newsletter subscribers to mailchimp using PHP

The MailChimp API (application programming interface) provides a way to work with campaigns and lists subscribers programmatically. Here, we will discusses how to use the MailChimp API (application programming interface) with PHP. This article assumes that you have already have a MailChimp account and created API key and having Audience id (List id).

Mailchimp API Key and URL

Here our API key is ” XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us3 “. if your API key is like this then your url will be https://us3.api.mailchimp.com/3.0.

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

Set the authorization header with your api key as below,

apikey XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us3

List Mailchimp account Lists ID using API Key like below

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://us3.api.mailchimp.com/3.0/lists",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "authorization: apikey xxxxxxxxxxxxxxxxxx-us3"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

//Output

{
    "lists": [
        {
            "id": "xxxxxx154",
            "web_id": 12345678,
            "name": "Your company name",
            "contact": {},
            "permission_reminder": "You are receiving this email because you opted in via our website.",
            //...
        }
    ]
}

Add Newsletter subscriber member to list

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

{
    "email_address":"test@gmail.com", 
    "status":"subscribed",  
    "merge_fields": {
        "FNAME": "User1",
        "LNAME": "XYZ"
    }
}

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

$audiance_ID = 'xxxxxx154';
$authToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us3';
// The data to send to the API

$post = array(
    "email_address" => $_POST["email"], 
    "status" => "subscribed", 
    "merge_fields" => array(
    "FNAME"=> $_POST["name"],
    "LNAME"=> $_POST["lname"])
);

$ch = curl_init('https://us3.api.mailchimp.com/3.0/lists/'.$audiance_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($post)
));
// Send the request
$response = curl_exec($ch);

//Output

{
    "id": "fhdfdjhfdg57486784748h",
    "email_address": "test@gmail.com",
    "unique_email_id": "gfdgfh656",
    "web_id": 435465777,
    "email_type": "html",
    "status": "subscribed",
    "merge_fields": {
        "FNAME": "User1",
        "LNAME": "XYZ"
    }
}

Thats all, using above code you will be able to subscribe member to mail

Leave a Reply