为什么函数sendMessage()在这里抛出异常?
$mg = new MailGun('my_actual_api_key');
$response = $mg->sendMessage('my-domain.com', array(
'from' => 'real@email.com',
'to' => 'real@email.com',
'subject' => 'Test',
'html' => '<h1>Test body</h2>'
));
我得到的例外是…
致命错误:未捕获异常"Mailgun''Connection''Exceptions''MissingRequiredParameters",消息为"传递给API的参数无效。检查您的输入!"在C:''wamp''www''sektor''admin''app''application''third_party''MailGun''vendor''MailGun''MailGun-php''src''MailGun''Connection''RestClient.php中,位于第127行
显然,我发送给API的参数是错误的,但这遵循了MailGun API文档,但这显然不起作用。
我根本没有修改Mailer类的代码。
要获得有关该错误的更多详细信息,请使用此补丁中的代码:
将实际响应消息添加到400、401和404响应代码。这提供了比当前消息。这条信息并没有给你太多的信息花了几个小时试图找出我做错了什么,反复检查我的API键并在谷歌上查找错误。
像这样更改源文件src/Mailgun/Connection/RestClient.php
(完整补丁位于https://github.com/mailgun/mailgun-php/pull/72/files):
当抛出异常EXCEPTION_MISSING_REQUIRED_PARAMETERS
时,使用方法getResponseExceptionMessage()
(注意添加和删除行前面的+和-符号)获取更多信息:
elseif($httpResponseCode == 400){
- throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS);
+ throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj));
}
/**
+ * @param 'Guzzle'Http'Message'Response $responseObj
+ * @return string
+ */
+ protected function getResponseExceptionMessage('Guzzle'Http'Message'Response $responseObj){
+ $body = (string)$responseObj->getBody();
+ $response = json_decode($body);
+ if (json_last_error() == JSON_ERROR_NONE && isset($response->message)) {
+ return " " . $response->message;
+ }
+ }