编程语言
首页 > 编程语言> > php – Mailgun sendMessage参数异常

php – Mailgun sendMessage参数异常

作者:互联网

为什么函数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>'
));

……而我得到的例外是……

Fatal error: Uncaught exception’Mailgun\Connection\Exceptions\MissingRequiredParameters’ with message ‘The parameters passed to the API were invalid. Check your inputs!’ in C:\wamp\www\sektor\admin\app\application\third_party\MailGun\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php on line 127

显然我发送给API的参数是错误的,但这是遵循MailGun API文档,但这显然不起作用.

我根本没有修改过Mailer类的代码.

解决方法:

要获得有关该错误的更多详细信息,请使用this patch中的代码:

Adds the actual response message to the errors thrown on 400, 401 and
404 response codes. This provides a lot more useful info than the
current messages. The message doesn’t really give you much to go on. I
spent hours trying to find what I did wrong, double checking my API
keys and looking up the error on google.

像这样更改源文件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;
+       }
+   }   

标签:mailgun,php,codeigniter
来源: https://codeday.me/bug/20190824/1711648.html