在 laravel 10 应用程序中使用 guzzlehttp/guzzle ^7.8.0 捕获 https://github.com/mailchimp/mailchimp-marketing-php (v3.0) 的 mailchimpListId 方法的运行时错误 库 getContents 方法出现错误:
try {
$response = $this->getApiClient()->lists->addListMember($this->mailchimpListId, [
"email_address" => $email,
"status" => "subscribed",
'merge_fields' => $userMergeFields,
]);
} catch (ClientException $e) {
\Log::info($e->getMessage());
\Log::info(varDump($e->getResponse()->getBody()->getContents());
返回包含所有 3 个字段的字符串:
[2024-02-18 07:18:45] local.INFO: scalar => (string) :{"title":"Member Exists","status":400,"detail":"[email protected] is already a list member. Use PUT to insert or update list members.","instance":"4d85bc54-136f-e637-92e2-3c1bf9f1048c"}
I try to get access to all 3 fields title", status, detail, but I did not find how can I do it. I tried like :
\Log::info(varDump($e->getResponse()->getBody()->getTitle(), ' -10 $e->getResponse()->getTitle()::'));
但出现错误:
Call to undefined method GuzzleHttp\Psr7\Response::getTitle()
我怎样才能访问这3个字段?
您收到的错误消息是 JSON 字符串。您可以使用 PHP 中的
json_decode
函数将此 JSON 字符串转换为关联数组。之后,您可以将字段 title
、status
和 detail
作为数组元素访问。
您可以这样做:
try {
$response = $this->getApiClient()->lists->addListMember($this->mailchimpListId, [
"email_address" => $email,
"status" => "subscribed",
'merge_fields' => $userMergeFields,
]);
} catch (ClientException $e) {
$errorResponse = json_decode($e->getResponse()->getBody()->getContents(), true);
\Log::info('Title: ' . $errorResponse['title']);
\Log::info('Status: ' . $errorResponse['status']);
\Log::info('Detail: ' . $errorResponse['detail']);
}