Skip to main content

Adding subscribers to a list using Mailchimp’s API v3

Based on the List Members Instance docs, the easiest way is to use a PUT request which according to the docs either “adds a new list member or updates the member if the email already exists on the list”.

Furthermore, API key is definitely not part of the JSON schema and there’s no point in including it in your JSON request.

Also, you can use CURLOPT_USERPWD for basic HTTP auth as illustrated in below.

I’m using the following function to add and update list members. You may need to include a slightly different set of merge_fields depending on your list parameters.

<?php
/**
 * Adding subscribers to a list using Mailchimp's API v3
 * Based on the List Members Instance docs,
 * @link http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/
 */
$data = [
    'email'     => '[email protected]',
    'status'    => 'subscribed',
    'firstname' => 'Angelina',
    'lastname'  => 'Jolie'
];
syncMailchimp($data);
function syncMailchimp($data) {
    $apiKey = 'your api key';
    $listId = 'your list id';
    $memberId = md5(strtolower($data['email']));
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
    $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
    $json = json_encode([
        'email_address' => $data['email'],
        'status'        => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
        'merge_fields'  => [
            'FNAME'     => $data['firstname'],
            'LNAME'     => $data['lastname']
        ]
    ]);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
    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_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $httpCode;
}

May the 4th be with you,
Alex