Updating subscribers in a list using cURL and Mailchimp API v3
Solution 1
I figured out where I'm going wrong. When the user is initially added to the list the response provides an ID. I need to store the ID in my database with those person's details and reference the ID in the url I'm making a call to when I want to update the user's details 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");
Solution 2
You're looking for the CURLOPT_CUSTOMREQUEST
option in cURL.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
But, since this is now your second question in as many days asking how to use built-in cURL library, it might be worth using something a little better. If you're on PHP 5.4 or better, I recommend Guzzle. PHP Requests is also very good, though, and works with PHP 5.3.
Related videos on Youtube
VenomRush
Updated on September 15, 2022Comments
-
VenomRush over 1 year
I have this code below that adds a user to a pre-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 add the details of the same user twice it throws the following error on the second attempt:
[email protected] is already a list member. Use PATCH to update existing members.
How do I go about using PATCH to update user details? I'm not sure where to specify it.
-
VenomRush almost 9 yearsThis doesn't seem to work. I get the following error: "type" : "kb.mailchimp.com/api/error-docs/405-method-not-allowed", "title" : "Method Not Allowed","status":405,"detail":"The requested method and resource are not compatible. See the Allow header for this resource's available methods." The link on the page of the provided url leads to a 403 page which doesn't help me much.
-
VenomRush almost 9 yearsAlso, thanks for those suggestions. I'll keep them in mind for future projects. The framework this website was developed in is pretty old and I think I'd spend more time figuring out how to integrate Guzzle than it's worth.
-
VenomRush almost 9 yearsComment fail, the url in my original comment is kb.mailchimp.com/api/error-docs/405-method-not-allowed
-
TooMuchPete almost 9 yearsDo note that the "subscriber ID" is really just the MD5 hash of their email address.
-
VenomRush almost 9 yearsGood to know. Thanks.
-
Bjørn Børresen over 8 yearsFor anyone else looking at how to update the email of an existing subscriber - you can't do that (you need to DELETE the old email and then add the new one as a new subscriber). You cannot change information marked as "readonly" in the schema here: us9.api.mailchimp.com/schema/3.0/Lists/Members/Instance.json
-
BadHorsie about 8 yearsAlso note the subscriber ID is the lower case email address hashed with MD5. In PHP this means you must do
md5(strtolower($email))
. Here is the MailChimp documentation link on this. -
mickmackusa about 4 yearsDid you notice that you set
PUT
then overwrote withPATCH
before executing? Bad copy-paste? -
mickmackusa about 4 yearsDid you need to check my linkedin before deciding to respond?