/** * Generic API Call, this is a internal used method for sending all requests to webservice and parse * JSON response and transforms to PHP-Array object. * @param ApiRequest $request API Abstract request helper for construct request * @return array Response data into PHP Array structure * @throws HotelSDKException Calling exception, can capture remote server auditdata if exists. */ private function callApi(ApiRequest $request) { try { $signature = hash("sha256", $this->apiKey . $this->sharedSecret . time()); $this->lastRequest = $request->prepare($this->apiKey, $signature); $response = $this->httpClient->send($this->lastRequest); } catch (\Exception $e) { throw new HotelSDKException("Error accessing API: " . $e->getMessage()); } if ($response->getStatusCode() !== 200) { $auditData = null; $message = ''; $errorResponse = null; if ($response->getBody() !== null) { try { $errorResponse = \Zend\Json\Json::decode($response->getBody(), \Zend\Json\Json::TYPE_ARRAY); $auditData = new AuditData($errorResponse["auditData"]); $message = $errorResponse["error"]["code"] . ' ' . $errorResponse["error"]["message"]; } catch (\Exception $e) { throw new HotelSDKException($response->getReasonPhrase() . ': ' . $response->getBody()); } } throw new HotelSDKException($response->getReasonPhrase() . ': ' . $message, $auditData); } return \Zend\Json\Json::decode(mb_convert_encoding($response->getBody(), 'UTF-8'), \Zend\Json\Json::TYPE_ARRAY); }
/** * @param ApiRequest $request API Abstract request helper for contruct request * @return mixed Response data into array format * @throws HotelSDKException * @throws \Exception */ private function callApi(ApiRequest $request) { try { $this->lastRequest = $request->prepare($this->apiKey, $this->signature); $response = $this->httpClient->send($this->lastRequest); } catch (\Exception $e) { throw new HotelSDKException("Error accessing API: " . $e->getMessage()); } if ($response->getStatusCode() === 403) { throw new \Exception("Not authorized, review your api-key and secret!"); } if ($response->getStatusCode() !== 200) { $auditData = null; $message = ""; if ($response->getBody() !== null) { $errorResponse = \Zend\Json\Json::decode($response->getBody(), \Zend\Json\Json::TYPE_ARRAY); $auditData = new AuditData($errorResponse["auditData"]); $message = $errorResponse["error"]["message"]; } throw new HotelSDKException($response->getReasonPhrase() . ". " . $message, $auditData); } return \Zend\Json\Json::decode($response->getBody(), \Zend\Json\Json::TYPE_ARRAY); }