Kalpit Kalpit - 8 months ago 169
PHP Question

how to add email address to mailchimp list using api call in php

i am newbie in mailchimp. just now i have created account and got api key. i have gone through their api for adding email address to the list but didn't help it.

i have contact us form and when user clicks on submit button i want to add user's email id in my mailchimp database.

i have tried with this code also but getting 104 error..

$apikey = '***********-us3';
$listID = '*******';
$email = "********";
$url = sprintf('https://us2.api.mailchimp.com/2.0/lists/subscribe&apikey=%s&id=%s&email_address=%s&output=json', $apikey, $listID, $email, $_SERVER['REMOTE_ADDR']);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$arr = json_decode($data, true);
curl_close($ch);
if ($arr == 1) {
echo 'Check now your e-mail and confirm your subsciption.';
} else {
echo $arr['code'];
switch ($arr['code']) {
case 214:
echo 'You are already subscribed.';
break;
// check the MailChimp API for more options
default:
echo 'Unkown error...';
break;
}
}


can anyone suggest me how to achieve it?

Thanks in advance

Answer Source

mailchimp provide a PHP wrapper at https://bitbucket.org/mailchimp/mailchimp-api-php which tends to make life a million times easier.

The problem what seems to be happening here is that you are doing a GET rather than a POST

try

$apikey = '**********-us3';
$listID = '******';
$email  = "**************";
$fields = array('apikey' => urlencode($apikey), 'id' => urlencode($listID), 'email_address' => urlencode($email), 'output' => 'json' );
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$url = 'https://us2.api.mailchimp.com/2.0/lists/subscribe';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$data = curl_exec($ch);
$arr = json_decode($data, true);
curl_close($ch);
if ($arr == 1) {
    echo 'Check now your e-mail and confirm your subsciption.';
} else {
                    echo $arr['code'];
    switch ($arr['code']) {
        case 214:
        echo 'You are already subscribed.';
        break;
        // check the MailChimp API for more options
        default:
        echo 'Unkown error...';
        break;          
    }
}

And let me know if that helped. If not I can see if I can try and set something up myself so I can test some code.

==============================================================

For Version 3

$apikey = '*************-us8'; // API KEY OF MAURICE ACCOUNT
$email = '[email protected]';
$fields = array('merge_fields' => array("FNAME" => 'name'),'email_address' => $email,'status'=>'subscribed'); 
$url = 'https://us8.api.mailchimp.com/3.0/lists/b9dd55747x/members';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Accept: application/vnd.api+json',
            'Content-Type: application/vnd.api+json',
            'Authorization: apikey ' .$apikey
        ));
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$data = curl_exec($ch);
$arr = json_decode($data, true);
curl_close($ch);

if ($arr == 1) {
    echo 'Check now your e-mail and confirm your subsciption.';
} else {
                    echo $arr['code'];
    switch ($arr['code']) {
        case 214:
        echo 'You are already subscribed.';
        break;
        // check the MailChimp API for more options
        default:
        echo 'Unkown error...';
        break;          
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download