PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 6, 2022

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

 September 06, 2022     curl, mailchimp, mailchimp-api-v3.0, php     No comments   

Issue

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.


Solution

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 .



Answered By - Manikandan Arunachalam
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing