Add Email To MailChimp List Php Curl Script

Ready to go curl call to add an email address to your Mailchimp list. Just paste it in your code and it will be ready to use.

$apiKey = 'YOUR-API-KEY';
$listId = 'YOUR-LIST-ID';
$memberId = md5(strtolower('abcd@example.com'));
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
    $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
    $json = json_encode(array(
        'email_address' => 'abcd@example.com',
        'status'        => 'subscribed', // "subscribed","unsubscribed","cleaned","pending"
        'merge_fields'  => array(
            'FNAME'     => 'Johny',
            'LNAME'     => 'cage'
        )
    ));
    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('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);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

    $result = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);

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

Note: If you still have any confusion regarding this integration. You can choose us to do this for you by clicking button below.

Integrate Your MailChimp With Us In Only $5