I'm working on adding a basic Mailchimp subscribe form to a website using their MailchimpMarketing\ApiClient() composer resource. Adding a user seems to work fine, but when trying to add somebody who already exists, i'd expect just a nice json response so that I can catch that error and display it to the user, but instead I get 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 doesnt really seem to explain much, I had to find the right way to catch that error from stack overflow too as Mailchimps documentation was.. lacking! Here 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();
}
Here is the mailchimp documentation I used: https://mailchimp.com/developer/api/marketing/list-members/add-member-to-list/
I could just catch the error code of 400 and output a custom error but i'm sure that I must be doing soemthing wrong to get such an unhelpful response from the Mailchimp API?
Many thanks, andy and all help is appreciated.