2

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.

ekad
  • 14,436
  • 26
  • 44
  • 46
Manikandan Arunachalam
  • 1,470
  • 3
  • 17
  • 32
  • The code you posted has a syntax error in it where the `$url` variable is being assigned. Also, your path doesn't have a slash at the beginning, which [MailChimp's documentation shows](http://developer.mailchimp.com/documentation/mailchimp/guides/how-to-use-batch-operations/). Assuming that's not the problem, how many operations is the batch operation showing are successful? Is that the number you expected? – TooMuchPete Mar 31 '16 at 18:51
  • I have updated the post with $url variable and path. Still getting the error. actually, I am getting success response for only the address which has already exist in the list, for each new address I am getting failed operation. – Manikandan Arunachalam Apr 01 '16 at 06:12
  • 1
    Is that response what comes back inside the successful batch call or what you get when you make the batch call? if the former, the most likely thing is that the list ID isn't correct. If you use something like runscope you'll be able to see what post body is being sent. – TooMuchPete Apr 01 '16 at 20:53
  • The response is what i made the request with the batchId which i got from the response of batch operation request. – Manikandan Arunachalam Apr 02 '16 at 14:55
  • Sounds like the list ID you're passing in is invalid. – TooMuchPete Apr 02 '16 at 17:09
  • No, ID is correct.I fixed it. But the list url is wrong. I dont know initially i was using correct url but that time i dont know whats wrong. But after as u said i check their documentation for list update, they said **/lists/listID/** but when i removed the first / it works.now the correct path is **lists/listID/..**. – Manikandan Arunachalam Apr 02 '16 at 17:23

2 Answers2

4

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

enter image description here

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 .

Manikandan Arunachalam
  • 1,470
  • 3
  • 17
  • 32
-1

Please remove following code and change the type to POST, problem solved.

array_push($batch_operations, $operation);
Clintu
  • 25
  • 4