简体   繁体   中英

Update existing contact in Mailchimp

Hello guys I am trying to update a contact in my list with CRUL. I think everything looks great, only one think that I don't understand is {subscriber_hash}. How can i get this? Or maybe thats not a problem

I get this response

Exists","status":400,"detail":"example@example.com is already a list member. Use PUT to insert or update list members.","instance":"a5de431b-3b5a-3149-1fe3-bc1f6d08ec24"}

But my code looks good i think

    $list_id = '[LISTID]';
    $authToken = '[APIKEY]';
    $postData = array(
        "email_address" => "example@example.com",
        "status" => "subscribed"
    );
    $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://us12.api.mailchimp.com/3.0/lists/'.$list_id.'/members/{subscriber_hash}',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => json_encode($postData),
  CURLOPT_HTTPHEADER => array(
    'Authorization: apikey '.$authToken,
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

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

This is the working code finally

$list_id = 'LISTID';
    $authToken = 'APIKEY';
    // The data to send to the API
    
    $postData = array(
        "email_address" => "example@example.com",
        "status" => "subscribed",
        "firstname" => "asd",
        "lastname" => "asda"
    );
    
    $data = $postData;
      
      function syncMailchimp($data) {
        $apiKey = 'APIKEY';
        $listId = '0LISTID';
      
        $memberId = md5(strtolower($data['email_address']));
        $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
        $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
      
        $json = json_encode([
            'email_address' => $data['email_address'],
            'status'        => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
            'merge_fields'  => [
                'FNAME'     => 'asd',
                'LNAME'     => 'asd'
            ]
        ]);
      
        $ch = curl_init($url);
      
        curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                                                                                                                 
      
        $result = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
      
        return $result;
      }
      
      $res = syncMailchimp($data);
      print_r($res);

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