Mailchimp邮箱订阅更新异常:已用PUT仍报成员已存在错误
Let's break down why you're seeing that "Member Exists" error even when using PUT, and how to fix it:
1. The Critical Issue: Undefined $email Variable
The biggest problem in your code is that you're using $email in the API endpoint URL, but this variable is never defined.
In this line:
$response = wp_remote_post( 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/XXXXX/members/' . md5(strtolower($email)), $args );
$email hasn't been assigned a value, so strtolower($email) returns an empty string. The MD5 hash of an empty string is d41d8cd98f00b204e9800998ecf8427e, which means you're sending the PUT request to the wrong endpoint—instead of targeting the existing member's unique URL, you're trying to create a new member at the endpoint for an empty email address. That's why Mailchimp throws the "Member Exists" error: the email is already in your list, but you're not updating the correct entry.
Fix this by defining $email right after you validate the form data:
if(count($data) > 0) { $email = $data["email"]; // Add this line $api_key = 'XXXXXXXXXXXXXX'; // Rest of your code... }
2. Using the Correct WordPress HTTP Function
While you set 'method' => 'PUT' in your args, you're using wp_remote_post—a function designed specifically for POST requests. Even though WordPress allows overriding the method in the args, it's more reliable and readable to use wp_remote_request for non-POST methods like PUT.
Replace your wp_remote_post call with:
$response = wp_remote_request( 'https://' . substr($api_key, strpos($api_key, '-') + 1) . '.api.mailchimp.com/3.0/lists/XXXXX/members/' . md5(strtolower($email)), $args );
3. Bonus: Add Error Handling for Failed Requests
It's a good practice to check if the HTTP request itself failed (e.g., network issues) before trying to process the response. Add this right after making the request:
if (is_wp_error($response)) { echo 'Request failed: ' . $response->get_error_message(); wp_die(); }
Corrected Full Code
Here's your code with all the fixes applied:
function mailchimp_ajax_subscription() { $data = isset($_POST['formData']) ? $_POST['formData'] : array(); ob_start(); if(count($data) > 0) { $email = $data["email"]; // Define the email variable $api_key = 'XXXXXXXXXXXXXX'; $status = 'unsubscribed'; // subscribed, unsubscribed, cleaned, pending $args = array( 'method' => 'PUT', 'headers' => array( 'Authorization' => 'Basic ' . base64_encode('user:' . $api_key) ), 'body' => json_encode(array( 'email_address' => $email, // Use the defined $email variable here too for consistency 'status' => $status, 'tags' => array($data["name"]) )) ); $response = wp_remote_request( 'https://' . substr($api_key, strpos($api_key, '-') + 1) . '.api.mailchimp.com/3.0/lists/XXXXX/members/' . md5(strtolower($email)), $args ); // Check for request errors first if (is_wp_error($response)) { echo 'Request failed: ' . $response->get_error_message(); wp_die(); } $body = json_decode($response['body']); if ($response['response']['code'] == 200 && $body->status == $status) { echo 'The user has been successfully ' . $status . '.'; } else { echo '<b>' . $response['response']['code'] . ' ' . $body->title . ':</b> ' . $body->detail; } } wp_die(); }
These changes should resolve the "Member Exists" error and allow you to update existing subscribers' status and tags properly.
内容的提问来源于stack exchange,提问作者Prashant Patil

