From 0e6e912176255f599836cd5c436eee2faa4dc5fd Mon Sep 17 00:00:00 2001 From: Evan Seguin Date: Thu, 20 Sep 2012 12:44:43 -0700 Subject: [PATCH] Fixing a certificate issue --- .../mashape/exceptions/ExceptionConstants.php | 4 +++ main/mashape/http/HttpClient.php | 16 ++++++------ main/mashape/http/MashapeResponse.php | 25 +++++++++++-------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/main/mashape/exceptions/ExceptionConstants.php b/main/mashape/exceptions/ExceptionConstants.php index e6385ee..783800b 100755 --- a/main/mashape/exceptions/ExceptionConstants.php +++ b/main/mashape/exceptions/ExceptionConstants.php @@ -50,6 +50,10 @@ define("EXCEPTION_GET_INVALID_CONTENTTYPE_CODE", 415); define("EXCEPTION_GET_INVALID_CONTENTTYPE", "A GET request must have a content" ." type of application/x-www-form-urlencoded or application/json"); +define("EXCEPTION_CURL_CODE", 520); +define("EXCEPTION_CURL", + "Encountered an exception making the request"); + define("EXCEPTION_SYSTEM_ERROR_CODE", 2000); define("EXCEPTION_JSONDECODE_REQUEST", "Can't deserialize the response JSON: %s"); diff --git a/main/mashape/http/HttpClient.php b/main/mashape/http/HttpClient.php index fda6585..1527e84 100755 --- a/main/mashape/http/HttpClient.php +++ b/main/mashape/http/HttpClient.php @@ -47,11 +47,7 @@ class HttpClient { } self::validateRequest($httpMethod, $url, $parameters, $authHandlers, $contentType); - $response = self::execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType); - - if ($encodeJson) { - $response->parseBodyAsJson(); - } + $response = self::execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType, $encodeJson); return $response; } @@ -97,7 +93,7 @@ class HttpClient { } } - private static function execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType) { + private static function execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType, $encodeJson) { // first, collect the headers and parameters we'll need from the authentication handlers list($headers, $authParameters) = HttpUtils::handleAuthentication($authHandlers); if (is_array($parameters)) { @@ -118,12 +114,18 @@ class HttpClient { curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt ($ch, CURLINFO_HEADER_OUT, true); + curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); + if (curl_error($ch)) { + throw new MashapeClientException( + EXCEPTION_CURL . ":" . curl_error($ch), + EXCEPTION_CURL_CODE); + } $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $responseHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT); curl_close($ch); - return new MashapeResponse($response, $httpCode, $responseHeaders); + return new MashapeResponse($response, $httpCode, $responseHeaders, $encodeJson); } } diff --git a/main/mashape/http/MashapeResponse.php b/main/mashape/http/MashapeResponse.php index 8e0c6ef..76ce484 100644 --- a/main/mashape/http/MashapeResponse.php +++ b/main/mashape/http/MashapeResponse.php @@ -34,22 +34,27 @@ class MashapeResponse { public $rawBody; public $headers; - function __construct($response, $statusCode, $headers) { + function __construct($response, $statusCode, $headers, $encodeJson = false) { $this->rawBody = $response; $this->headers = $headers; $this->statusCode = $statusCode; + $this->_parseBody($encodeJson); } - function parseBodyAsJson() { - $this->body = json_decode($this->rawBody); - if (empty($this->body) && ($this->statusCode == 200)) { - // It may be a chunked response - //$this->body = json_decode(http_chunked_decode($this->rawBody)); - if (empty($this->body)) { - throw new MashapeClientException( - sprintf(EXCEPTION_JSONDECODE_REQUEST, $this->rawBody), - EXCEPTION_SYSTEM_ERROR_CODE); + function _parseBody($encodeJson) { + if ($encodeJson) { + $this->body = json_decode($this->rawBody); + if (empty($this->body) && ($this->statusCode == 200)) { + // It may be a chunked response + //$this->body = json_decode(http_chunked_decode($this->rawBody)); + if (empty($this->body)) { + throw new MashapeClientException( + sprintf(EXCEPTION_JSONDECODE_REQUEST, $this->rawBody), + EXCEPTION_SYSTEM_ERROR_CODE); + } } + } else { + $this->body = $this->rawBody; } } }