这个问题在这里已经有了答案:
Guzzlehttp - How get the body of a response from Guzzle 6?
(3 个回答)
Handle Guzzle exception and get HTTP body
(9 个回答)
27 天前关闭。
我正在使用他们的 MailchimpMarketing\ApiClient() composer 资源向网站添加一个基本的 Mailchimp 订阅表单。添加用户似乎工作正常,但是当尝试添加已经存在的人时,我希望只有一个很好的 json 响应,以便我可以捕获该错误并将其显示给用户,但是我得到以下 GuzzleHttp\Exception\客户端异常:
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...)
该文档似乎并没有真正解释太多,我也必须找到正确的方法来从堆栈溢出中捕获该错误,因为 Mailchimps 文档是......缺乏!这是代码: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();
}
这是我使用的 mailchimp 文档:https://mailchimp.com/developer/api/marketing/list-members/add-member-to-list/我可以捕获 400 的错误代码并输出自定义错误,但我确定我一定是做错了什么才能从 Mailchimp API 获得如此无益的响应?
非常感谢,安迪和所有帮助表示赞赏。
最佳答案
我遇到了同样的问题,所以我最终通过将最后一个 catch 子句更改为以下内容来指示错误消息以查看整个错误是什么:
catch (GuzzleHttp\Exception\ClientException $e) {
echo '<pre>' . var_export($e->getResponse()->getBody()->getContents()).'</pre>';
$errors[] = $e->getMessage();
}
这揭示了以下糟糕的错误,到目前为止已被截断:{
"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"
}
所以,我的问题是 @example.com
看起来是假的或无效的...尝试使用不同的电子邮件,甚至是
@test.com
工作。我不能向你保证这也会是你的问题,但检查错误详细信息绝对是一个好的第一步!
关于php - Mailchimp addListMember 在已经存在时返回客户端错误 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63722005/