Updating list subscribers using cURL and Mailchimp API v3

I have this code below that adds the user to an existing list in Mailchimp.

$apikey = '<api_key>'; $auth = base64_encode( 'user:'.$apikey ); $data = array( 'apikey' => $apikey, 'email_address' => $email, 'status' => 'subscribed', 'merge_fields' => array( 'FNAME' => $name ) ); $json_data = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.$auth)); curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); $result = curl_exec($ch); var_dump($result); die('Mailchimp executed'); 

This code only adds users to the list, and when I try to add the data of the same user, it causes the following error a second time:

test@user.com already a member of the list. Use PATCH to update existing items.

How can I use PATCH to update user data? I'm not sure where to point it.

+5
source share
3 answers

I understood where I am going wrong. When a user is initially added to the list, the response provides an identifier. I need to save the identifier in my database with the data of this person and specify the identifier in the URL that I am calling when I want to update the user data in the Mailchimp list.

 https://us2.api.mailchimp.com/3.0/lists/<list_id_goes_here>/members/<members_id_goes_here> 

Thanks @TooMuchPete for the correct curl command.

 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); 
+5
source

You are looking for the parameter CURLOPT_CUSTOMREQUEST in cURL .

 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); 

But, since this is your second question for many days, when you ask how to use the built-in cURL library, it may be worth improving a little. If you are using PHP 5.4 or better, I recommend Guzzle . PHP queries are also very good and works with PHP 5.3.

+3
source

Try it. This works for me. I am using this feature. Hope it should solve your problem.

 <?php /** * Created by PhpStorm. * User: Faisal * Website: www.faisal-ibrahim.info * Date: 2/12/2016 * Time: 10:07 AM */ if (isset($_POST['email'])) { $email = $_POST['email']; } else { $email = ' faisal.im048@gmail.com '; } $data = [ 'email' => $email, 'status' => 'subscribed', 'firstname' => 'Faisal', 'lastname' => 'Ibrahim' ]; $api_response_code = listSubscribe($data); echo $api_response_code; /** * Mailchimp API- List Subscribe added function.In this method we'll look how to add a single member to a list using the lists/subscribe method.Also, We will cover the different parameters for submitting a new member as well as passing in generic merge field information. * * @param array $data Subscribe information Passed. * * @return mixed */ function listSubscribe(array $data) { $apiKey = "cf8a1fd222a500f27f9e042449867c7c-us15";//your API key goes here $listId = "e8f3f5f880";// your trageted list ID $memberId = md5(strtolower($data['email'])); $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'], 'status' => $data['status'], // "subscribed","unsubscribed","cleaned","pending" 'merge_fields' => [ 'FNAME' => $data['firstname'], 'LNAME' => $data['lastname'] ] ]); $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, 'PUT'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $httpCode; } 
0
source

All Articles