Mailgun sendMessage parameter exception

梦想与她 提交于 2019-12-05 19:48:32
Ivan Jovović

To get a bit more details about that error, use the code from 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.

Change source file src/Mailgun/Connection/RestClient.php like this (full patch is at https://github.com/mailgun/mailgun-php/pull/72/files):

When throwing exception EXCEPTION_MISSING_REQUIRED_PARAMETERS, get more info with the method getResponseExceptionMessage() (note + and - signs in front of added and removed lines):

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;
+       }
+   }   
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!