I'm using the MailchimpMarketing\ApiClient() writer resource to add a basic Mailchimp subscription form to the website. Adding a user seems to be working fine, but when trying to add an already existing user, I'm expecting just a nice json response so that I can catch that error and display it to the user, but I'm getting the following GuzzleHttp\Exception\ClientException :
Client error: `POST https://us10.api.mailchimp.com/3.0/lists/xxxxxxxxxx/members` resulted in a `400 Bad Request` response: {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status" (truncated...)
The documentation doesn't really seem to explain much, I also have to find the right way to catch errors on stack overflow because the Mailchimps documentation is... lacking! This is the code:
try { $response = $mailchimp->lists->addListMember($this->settings_helper->get('mailchimp_list_id'), [ "email_address" => $form->get_field_value('email'), "status" => "subscribed", "merge_fields" => [ "FNAME" => $first_name, "LNAME" => $last_name ] ]); if ($response->getId()) { $this->add_json_success($this->settings_helper->get('mailchimp_success_message')); } } catch (MailchimpMarketing\ApiException $e) { $errors[] = $e->getMessage(); } catch (ClientErrorResponseException $e) { $errors[] = $e->getMessage(); } catch (GuzzleHttp\Exception\ClientException $e) { $errors[] = $e->getMessage(); }
This is the mailchimp documentation I use: https://mailchimp.com/developer/api/marketing/list-members/add-member-to-list/
I could catch the 400 error code and output a custom error, but I'm sure I must be doing something wrong to get such a useless response from the Mailchimp API?
Thanks a lot, Andy, for all the help.
P粉7978557902024-02-26 00:09:15
I had the same problem so I ended up indicating the error message by changing the last catch clause to the following to see the entire error:
catch (GuzzleHttp\Exception\ClientException $e) { echo '' . var_export($e->getResponse()->getBody()->getContents()).''; $errors[] = $e->getMessage(); }
This reveals the following bad error, which has been truncated so far:
{ "type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/", "title":"Invalid Resource", "status":400, "detail":"Isabell_Murazik@example.com looks fake or invalid, please enter a real email address.", "instance":"fcc1d762-2475-40a6-bc7f-4ac7f3fb7902" }
So, my question is @example.com
looks fake or invalid...
Tried different emails, even @test.com
worked.
I can't guarantee you that this will be your problem as well, but checking the error details is definitely a good first step!