简体   繁体   中英

How to create/update bulk of emails to Mailchimp list using their api 3.0?

I have a list that already created in MailChimp, and it has some addresses in the subscribed and unsubscribed lists.

Now, I need to create/update list of subscribers using api in PHP code.

$apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXX-us12";

$subscribers = array(array(
    'email_address'     => 'subscriber1@gmail.com',
    'status'    => 'subscribed',
    'merge_fields'  => array(
            'FNAME'     => "subscriber1F",
            'LNAME'     => "Arunachalam1L"
        )
),
array(
    'email_address'     => 'subscriber2@gmail.com',
    'status'    => 'subscribed',
    'merge_fields'  => array(
            'FNAME'     => "subscriber2F",
            'LNAME'     => "subscriber2L"
        )
));

$listId = "b633deb4c8";

$url = "https://us12.api.mailchimp.com/3.0/batches";
$id = 1;
    foreach ($subscribers as $subscriber) {
        #echo $subscriber['email_address'];
        $operation = array(
            'method'=>'PUT',
            'path'=>'/lists/'.$listId.'/members/'.md5(strtolower($subscriber['email_address'])),
            'body'=>json_encode($subscriber));
        $id++;
        array_push($batch_operations, $operation);
    }

    $request_encoded = json_encode(array('operations'=>$batch_operations));

    $curl = curl_init($url);

    curl_setopt($curl, CURLOPT_USERPWD, 'user:' . $apiKey);    
    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//raw output
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl, CURLOPT_POSTFIELDS, $request_encoded); 
$result = curl_exec($curl);

I am getting response 200. and then even I tried by getting the response for the BatchId which is returned after the batch operation submitted.

It returns operations finished,and all are successful but the list has not been updated. Has anyone successfully used their batches api?

Edit1: I am getting this response for the operation which has a new email address.

{"status_code":404,"operation_id":null,"response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/\",\"title\":\"Resource Not Found\",\"status\":404,\"detail\":\"The requested resource could not be found.\",\"instance\":\"\"}"}

Edit 2

Explanation Sorry I don't understand exactly what you mean. But what i am doing is first send the batch request,(for ex:- it has 2 operation as in the post).Then I am getting a BatchId in the response for the request I made. then I made a Get request with the BatchId, for this i am getting a response which has information of errored operation.I got a link to get the response of the batch operation with the results of the all the addresses i sent. which just has all operation but if the address is already exist then that operation is successful(in the sense just its added into the successful operation count) but the changes i made is not reflected, and also if the address is not exist that is added up in the failed operation.

I fixed the problem. Its because of the url path.Though Mailchimp documentation says like below(even i am trying batch operation) docs ,

在此处输入图片说明

I changed the path in the operation, it should be like this.

'path'=>'lists/'.$listId.'/members/'.md5(strtolower($subscriber['email_address'])),

now its working as expected. Have tried with 'PUT' method in operation, it does both creates/updates subscribers to the list in mailchimp .

请删除以下代码并将类型更改为POST,问题已解决。

array_push($batch_operations, $operation);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM