/** * @param Zend_Http_Response $response * @return json string * @throws */ public function parseZendResponse(Zend_Http_Response $response) { if ($response->getStatus() == 200) { return $response->getBody(); } else { throw new Exception("Error: Status is: " . $response->getStatus() . " message: " . $response->getMessage()); } }
/** * * * @param Zend_Http_Response $response JSON response from the PinPayments gateway * @throws Dwyera_Pinpay_Model_ResponseParseException If an invalid JSON response object is passed */ public function __construct(Zend_Http_Response $response) { $this->response = $response; $this->httpResponseCode = $response->getStatus(); $this->msgObj = json_decode($response->getBody()); if ($this->msgObj == null) { throw new Dwyera_Pinpay_Model_ResponseParseException("Could not parse PinPayments gateway response"); } }
/** * Sends a request and returns a response * * @param CartRecover_Request $request * @return Cart_Recover_Response */ public function sendRequest(CartRecover_Request $request) { $this->client->setUri($request->getUri()); $this->client->setParameterGet($request->getParams()); $this->client->setMethod($request->getMethod()); $this->client->setHeaders('Accept', 'application/json'); $this->response = $this->client->request(); if ($this->response->getHeader('Content-Type') != 'application/json') { throw new CartRecover_Exception_UnexpectedValueException("Unknown response format."); } $body = json_decode($this->response->getBody(), true); $response = new CartRecover_Response(); $response->setRawResponse($this->response->asString()); $response->setBody($body); $response->setHeaders($this->response->getHeaders()); $response->setStatus($this->response->getMessage(), $this->response->getStatus()); return $response; }
/** * (non-PHPdoc) * @see Tid_Rest_Processor_Input_Abstract::processInputData() */ public function processInputData(Zend_Http_Response $response = null) { try { return isset($response) ? Zend_Json::decode($response->getBody()) : array(); } catch (Exception $e) { $this->getRestService()->log("Error parsing JSON response: " . $e->getMessage(), Zend_Log::ERR); $this->getRestService()->log($e, Zend_Log::ERR); throw new App_Rest_Processor_Input_Exception('Error response: [code] ' . $response->getStatus() . ' [error] ' . $response->getMessage()); } }
/** * Retrieve latitude and longitude from the API response * * @param Zend_Http_Response|boolean $response * @return array|boolean */ protected function _parseResponse($response) { if ($response->isSuccessful() && $response->getStatus() == 200) { $_response = json_decode($response->getBody()); $_coordinates = $_response->results[0]->geometry->location; $geo = array('lat' => $_coordinates->lat, 'lng' => $_coordinates->lng); return $geo; } return false; }
/** * @param $ticketId * @return array|mixed */ public function listComments($ticketId) { try { $this->init()->setMethod('desk/comments')->addFilter('ticket', $ticketId)->doRequest(); } catch (Exception $e) { Mage::logException($e); } if ($this->result->getStatus() == 200) { return json_decode($this->result->getBody()); } return array(); }
public function processResponse(Zend_Http_Response $response) { $serviceName = $this->getRestService()->getServiceName(); if (!$serviceName) { throw new \Application\Exceptions\UnexpectedException("Service Name doesn't exist"); } $this->checkAlarmTimeSpent($serviceName); if ($response->isSuccessful()) { App::alarm()->notifyInvalidHttpCode($serviceName); } else { App::alarm()->notifyInvalidHttpCode($serviceName, $response->getStatus()); } }
/** * Makes a request to Recensus to contact a customer who has made a recent * purchase with a merchant. An email will be sent to the customer asking * them to review the product. Note: The email is not sent immediatley, * Recensus will determine the interval. * * */ public function makeCustomerContactRequest($data) { $url = $this->baseUrl . str_replace('{merchantId}', $this->merchantId, $this->endpoints['ccr']); $this->makeRequest($url, 'POST', $data); if ($this->wasSuccess()) { return $this->parseResponse(); } else { $code = $this->lastResponse->getStatus(); $body = $this->lastResponse->getBody(); $errorStr = $code . ': ' . $body; $this->handleError($errorStr); } return false; }
/** * Retrieve latitude and longitude from the API response * * @param Zend_Http_Response|boolean $response * @return array|boolean */ protected function _parseResponse($response) { if ($response->isSuccessful() && $response->getStatus() == 200) { $_response = json_decode($response->getBody()); if (is_array($_response->postalcodes)) { $_response = array_shift($_response->postalcodes); } if ($_response) { $geo = array('lat' => $_response->lat, 'lng' => $_response->lng); } else { $geo = false; } return $geo; } return false; }
protected function _processProtoMessage(Zend_Http_Response $response = null) { if (isset($this->getRestMethod()->inputProtoMessageClass) && substr((string) $response->getStatus(), 0, 1) == '2') { $protoMessageClass = $this->getRestMethod()->inputProtoMessageClass; try { $response = new $protoMessageClass($response->getBody()); return $response; } catch (Exception $e) { $this->getRestService()->log("Error loading proto message class: " . $protoMessageClass, Zend_Log::ERR); $this->getRestService()->log($e, Zend_Log::ERR); throw $e; } } //Errors which are taking place on input processing phase should throw //this Exception Subclass throw new App_Rest_Processor_Input_Exception('Error response: [code] ' . $response->getStatus() . ' [error] ' . $response->getMessage()); }
/** * Return the response data for client errors - 4XX range errors. * @param Zend_Http_Response $response * @return array */ public function returnClientErrorResponse(Zend_Http_Response $response) { $status = $response->getStatus(); switch ($status) { case 401: $message = self::INVALID_API_KEY; break; case 403: $message = self::INVALID_STORE_ID; break; case 408: $message = self::NETWORK_TIMEOUT; break; default: $message = self::UNKNOWN_FAILURE; break; } return array('message' => Mage::helper('eb2ccore')->__($message), 'success' => false); }
/** * Extract the API response data from the given HTTP response object. * * @param Zend_Http_Response $response * * @return $this */ protected function parseRawResponse(Zend_Http_Response $response) { if ($response->isSuccessful()) { $content = $response->getBody(); if (strlen($content) > 0) { try { $xml = simplexml_load_string($response->getBody()); } catch (Exception $e) { // Failed to parse XML $this->successful = false; $this->setMessage("Failed to parse a response from Klevu."); Mage::helper('klevu_search')->log(Zend_Log::ERR, sprintf("Failed to parse XML response: %s", $e->getMessage())); return $this; } $this->xml = $xml; $this->successful = true; } else { // Response contains no content $this->successful = false; $this->setMessage('Failed to parse a response from Klevu.'); Mage::helper('klevu_search')->log(Zend_Log::ERR, "API response content is empty."); } } else { // Unsuccessful HTTP response $this->successful = false; switch ($response->getStatus()) { case 403: $message = "Incorrect API keys."; break; case 500: $message = "API server error."; break; case 503: $message = "API server unavailable."; break; default: $message = "Unexpected error."; } $this->setMessage(sprintf("Failed to connect to Klevu: %s", $message)); Mage::helper('klevu_search')->log(Zend_Log::ERR, sprintf("Unsuccessful HTTP response: %s %s", $response->getStatus(), $response->responseCodeAsText($response->getStatus()))); } return $this; }
protected function createResponse(\Zend_Http_Response $response) { $headers = array($response->getHeader('Set-Cookie')); $cookies = array(); foreach ($headers as $header) { if (!trim($header)) { continue; } $parts = explode(';', $header); $value = array_shift($parts); list($name, $value) = explode('=', trim($value)); $cookies[$name] = array('value' => $value); foreach ($parts as $part) { list($key, $value) = explode('=', trim($part)); $cookies[$name][$key] = $value; } } return new Response($response->getBody(), $response->getStatus(), $response->getHeaders(), $cookies); }
/** * Parse response object and check for errors * * @param \Zend_Http_Response $response * @return stdClass */ protected function _parseResponse(\Zend_Http_Response $response) { if ($response->isError()) { switch ($response->getStatus()) { case 401: throw new Exception('Postmark request error: Unauthorized - Missing or incorrect API Key header.'); break; case 422: $error = \Zend_Json::decode($response->getBody()); if (is_object($error)) { throw new Exception(sprintf('Postmark request error: Unprocessable Entity - API error code %s, message: %s', $error->ErrorCode, $error->Message)); } else { throw new Exception(sprintf('Postmark request error: Unprocessable Entity - API error code %s, message: %s', $error['ErrorCode'], $error['Message'])); } break; case 500: throw new Exception('Postmark request error: Postmark Internal Server Error'); break; default: throw new Exception('Unknown error during request to Postmark server'); } } return \Zend_Json::decode($response->getBody()); }
/** * Process the response * * @param Zend_Http_Response $response * @return array * @throws Zend_Service_Exception */ protected function _processHttpResponse(Zend_Http_Response $response) { // Hack for logging if ($this->_log instanceof Zend_Log) { $client = $this->getHttpClient(); $this->_log->log(sprintf("Request:\n%s\nResponse:\n%s\n", $client->getLastRequest(), $client->getLastResponse()->asString()), Zend_Log::DEBUG); } // Check response body $responseData = $response->getBody(); if (!is_string($responseData) || '' === $responseData) { throw new Engine_Service_2Checkout_Exception('HTTP Client returned an ' . 'empty response', 'IS_EMPTY'); } // These are only supported using json if ('json' === $this->_format) { // Decode response body $responseData = Zend_Json::decode($responseData, Zend_Json::TYPE_ARRAY); if (!is_array($responseData)) { throw new Engine_Service_2Checkout_Exception('HTTP Client returned ' . 'invalid JSON response', 'NOT_VALID'); } // Check for special global error keys if (!empty($responseData['errors'])) { foreach ($responseData['errors'] as $message) { throw new Engine_Service_2Checkout_Exception(sprintf('API Error: ' . '[%1$s] %2$s', $message['code'], $message['message']), $message['code']); } } // Check for warnings if (!empty($responseData['warnings'])) { foreach ($responseData['warnings'] as $message) { throw new Engine_Service_2Checkout_Exception(sprintf('API Warning: ' . '[%1$s] %2$s', $message['code'], $message['message']), $message['code']); } } // Check for response status and message if ('OK' !== $responseData['response_code']) { throw new Engine_Service_2Checkout_Exception(sprintf('Response Error: ' . '[%1$s] %2$s', $responseData['response_code'], $responseData['response_message']), $responseData['response_code']); } } // Check HTTP Status code if (200 !== $response->getStatus()) { // Note: looks like 2checkout gives 400 for invalid parameters throw new Engine_Service_2Checkout_Exception(sprintf('HTTP Client ' . 'returned error status: %1$d', $response->getStatus()), 'HTTP'); } return $responseData; }
/** * @param Zend_Http_Response $response * @return bool */ protected function _handleAuthorizationResponse($response) { $status = $response->getStatus(); switch ($status) { case 401: $this->_getAccessToken(true); return self::RESPONSE_RETRY; case 400: case 404: return self::RESPONSE_FAIL; default: return self::RESPONSE_SUCCESS; } }
public function processResponse(Zend_Http_Response $response) { $this->_lastMessageSize = strlen($response->getRawBody()); $this->_status = $response->getStatus(); }
/** * Search for error from request. * * If any error is found a DOMDocument is returned, this object contains a * DOMXPath object as "ebayFindingXPath" attribute. * * @param Zend_Http_Response $response * @throws Zend_Service_Ebay_Trading_Exception When any error occurrs during request * @return SimpleXMLElement */ protected function _parseResponse(Zend_Http_Response $response) { // error message $message = ''; $xml = simplexml_load_string($response->getBody()); if (!$xml) { $message = 'It was not possible to load XML returned.'; } if ($xml->Ack != 'Success' && $xml->Ack != 'Warning') { $message = "Ack not success: " . print_r($xml, true); } //var_dump($dom->Ack);die; // second trying, check request status if ($response->isError()) { $message = $response->getMessage() . ' (HTTP status code #' . $response->getStatus() . ')'; } // throw exception when an error was detected if (strlen($message) > 0) { /** * @see Zend_Service_Ebay_Finding_Exception */ require_once 'Zend/Service/Ebay/Shopping/Exception.php'; throw new Zend_Service_Ebay_Shopping_Exception($message); } /* $dom = new DOMDocument(); $dom->loadXML($response->getBody()); Zend_Debug::dump($dom->saveXML()); Zend_Debug::dump($xml); */ return $xml; }
/** Get the status of the response * @access public * @param Zend_Http_Response $response * @return int */ private function getStatus(Zend_Http_Response $response) { $code = $response->getStatus(); switch ($code) { case $code == 200: $http = 200; break; case $code == 400: $http = 400; break; case $code == 404: $http = 404; break; case $code == 406: $http = 406; break; default: $http = 999; break; } return $http; }
/** * Prepare a solarium response from the given request and client * response * * @throws HttpException * @param Request $request * @param \Zend_Http_Response $response * @return Response */ protected function prepareResponse($request, $response) { if ($response->isError()) { throw new HttpException($response->getMessage(), $response->getStatus()); } if ($request->getMethod() == Request::METHOD_HEAD) { $data = ''; } else { $data = $response->getBody(); } // this is used because getHeaders doesn't return the HTTP header... $headers = explode("\n", $response->getHeadersAsString()); return new Response($data, $headers); }
/** * Checks ReST response for errors. * * @param Zend_Http_Response $response the ReST response * @return void * @throws Zend_Service_Technorati_Exception * @access protected */ protected static function _checkResponse(Zend_Http_Response $response) { if ($response->isError()) { throw new Zend_Service_Technorati_Exception(sprintf('Invalid response status code (HTTP/%s %s %s)', $response->getVersion(), $response->getStatus(), $response->getMessage())); } }
/** * Search for error from request. * * If any error is found a DOMDocument is returned, this object contains a * DOMXPath object as "ebayFindingXPath" attribute. * * @param Zend_Http_Response $response * @throws Zend_Service_Ebay_Trading_Exception When any error occurrs during request * @return SimpleXMLElement */ protected function _parseResponse(Zend_Http_Response $response) { // error message $message = ''; // first trying, loading XML $xml = simplexml_load_string($response->getBody()); if (!$xml) { $message = 'It was not possible to load XML returned.'; } if ($xml->Ack != 'Success') { $message = 'Ack not success.'; } // second trying, check request status if ($response->isError()) { $message = $response->getMessage() . ' (HTTP status code #' . $response->getStatus() . ')'; } // throw exception when an error was detected if (strlen($message) > 0) { /** * @see Zend_Service_Ebay_Finding_Exception */ require_once 'Zend/Service/Ebay/Trading/Exception.php'; throw new Zend_Service_Ebay_Trading_Exception($message); } return $xml; }
/** * Transfoms the response body (xml or json) into an array we can more easily * work with. * * @param Zend_Http_Response $response * @return array * @todo $this->_errors is populated with errors from Chargify. Should this * also populate a separate errors array when we get HTTP 404s or 201s? */ public function getResponseArray(Zend_Http_Response $response) { $return = array(); $format = $this->getService()->getFormat(); $body = $response->getBody(); $body = trim($body); /** * return early on bad status codes */ $code = $response->getStatus(); $errorCodes = array(404, 401, 500); if (in_array($code, $errorCodes)) { $this->_errors['Crucial_Service_Chargify']['Bad status code'] = $code; return $return; } /** * Return early if we have an empty body, which we can't turn into an array * anyway. This happens in cases where the API returns a 404, and possibly * other status codes. */ if (empty($body)) { return $return; } if ('json' == $format) { $return = Zend_Json::decode($body); } if ('xml' == $format) { $json = Zend_Json::fromXml($body); $return = Zend_Json::decode($json); } // set errors, if any if (!empty($return['errors'])) { $this->_errors = $return['errors']; } return $return; }
/** * Process the response * * @param Zend_Http_Response $response * @return array * @throws Zend_Service_Exception */ protected function _processHttpResponse(Zend_Http_Response $response) { // Hack for logging if ($this->_log instanceof Zend_Log) { $client = $this->getHttpClient(); $this->_log->log(sprintf("Request:\n%s\nResponse:\n%s\n", $client->getLastRequest(), $client->getLastResponse()->asString()), Zend_Log::DEBUG); } // Check HTTP Status code if (200 !== $response->getStatus()) { throw new Engine_Service_PayPal_Exception(sprintf('HTTP Client ' . 'returned error status: %1$d', $response->getStatus()), 'HTTP'); } // Check response body $responseStr = $response->getBody(); if (!is_string($responseStr) || '' === $responseStr) { throw new Engine_Service_PayPal_Exception('HTTP Client returned an ' . 'empty response', 'IS_EMPTY'); } // Decode response body $responseData = array(); foreach (explode("&", $responseStr) as $tmp) { $tmp = explode('=', $tmp, 2); if (count($tmp) > 1) { $responseData[urldecode($tmp[0])] = urldecode($tmp[1]); } } // Check for valid response if (!is_array($responseData) || empty($responseData) || count($responseData) <= 0 || !array_key_exists('ACK', $responseData)) { throw new Engine_Service_PayPal_Exception('HTTP Client returned ' . 'invalid NVP response', 'NOT_VALID'); } // Check for response status and message if (strtolower($responseData['ACK']) == 'failure') { switch (strtolower($responseData['L_SEVERITYCODE0'])) { default: case 'error': $level = Zend_Log::ERR; break; } throw new Engine_Service_PayPal_Exception(sprintf('API Error: ' . '[%1$d] %2$s - %3$s', $responseData['L_ERRORCODE0'], $responseData['L_SHORTMESSAGE0'], $responseData['L_LONGMESSAGE0']), $responseData['L_ERRORCODE0']); } return $responseData; }
/** * "Helper" function that check for the 200 status and add a success or error message * * @param Zend_Http_Response $httpResp * @param string $successMsg * @param string $errorMsg */ protected function _checkResponse($httpResp, $successMsg, $errorMsg = "There has been an error related to the Varnish Cache BAN.") { if ($httpResp->getStatus() == 200) { $this->_getSession()->addSuccess($successMsg); } else { $this->_getSession()->addError($errorMsg); } }
/** * Search for error from request. * * If any error is found a DOMDocument is returned, this object contains a * DOMXPath object as "ebayFindingXPath" attribute. * * @param Zend_Http_Response $response * @link http://developer.ebay.com/DevZone/finding/CallRef/types/ErrorSeverity.html * @see Zend_Service_Ebay_Finding_Abstract::_initXPath() * @throws Zend_Service_Ebay_Finding_Exception When any error occurrs during request * @return DOMDocument */ protected function _parseResponse(Zend_Http_Response $response) { // error message $message = ''; // first trying, loading XML $dom = new DOMDocument(); if (!($dom = @Zend_Xml_Security::scan($response->getBody(), $dom))) { $message = 'It was not possible to load XML returned.'; } // second trying, check request status if ($response->isError()) { $message = $response->getMessage() . ' (HTTP status code #' . $response->getStatus() . ')'; } // third trying, search for error message into XML response // only first error that contains severiry=Error is read $xpath = new DOMXPath($dom); foreach (self::$_xmlNamespaces as $alias => $uri) { $xpath->registerNamespace($alias, $uri); } $ns = self::XMLNS_FINDING; $nsMs = self::XMLNS_MS; $expression = "//{$nsMs}:errorMessage[1]/{$ns}:error/{$ns}:severity[.='Error']"; $severityNode = $xpath->query($expression)->item(0); if ($severityNode) { $errorNode = $severityNode->parentNode; // ebay message $messageNode = $xpath->query("//{$ns}:message[1]", $errorNode)->item(0); if ($messageNode) { $message = 'eBay error: ' . $messageNode->nodeValue; } else { $message = 'eBay error: unknown'; } // ebay error id $errorIdNode = $xpath->query("//{$ns}:errorId[1]", $errorNode)->item(0); if ($errorIdNode) { $message .= ' (#' . $errorIdNode->nodeValue . ')'; } } // throw exception when an error was detected if (strlen($message) > 0) { /** * @see Zend_Service_Ebay_Finding_Exception */ require_once LIB_DIR . '/Zend/Service/Ebay/Finding/Exception.php'; throw new Zend_Service_Ebay_Finding_Exception($message); } // add xpath to dom document // it allows service_ebay_finding classes use this $dom->ebayFindingXPath = $xpath; return $dom; }
/** Get the status for a response * @access public * @param Zend_Http_Response $response * @return boolean * @throws Exception */ public function getStatus(Zend_Http_Response $response) { $code = $response->getStatus(); switch ($code) { case $code == 200: return true; break; case $code == 400: throw new Exception('A valid appid parameter is required for this resource'); break; case $code == 404: throw new Exception('The resource could not be found'); break; case $code == 406: throw new Exception('You asked for an unknown representation'); break; default: return false; } }
/** * Initializes response object. * * @param Zend_Http_Response $response * * @return Jirafe_HttpConnection_Response */ protected function initializeResponse(Zend_Http_Response $response) { return new Jirafe_HttpConnection_Response($response->getBody(), $response->getHeaders(), $response->isError() ? $response->getStatus() : 0, $response->getMessage()); }
/** * Checks ReST response for errors. * * @param Zend_Http_Response $response the ReST response * @return void * @throws Zend_Service_Technorati_Exception * @access protected */ protected static function _checkResponse(Zend_Http_Response $response) { if ($response->isError()) { /** * @see Zend_Service_Technorati_Exception */ // require_once 'Zend/Service/Technorati/Exception.php'; throw new Zend_Service_Technorati_Exception(sprintf('Invalid response status code (HTTP/%s %s %s)', $response->getVersion(), $response->getStatus(), $response->getMessage())); } }
/** * Check status code and if appropriate return the decoded JSON * * @param Zend_Http_Response $response * @return array * @throws Zend_Service_Exception */ protected function _processResult(Zend_Http_Response $response) { if ($response->getStatus() != 200) { require_once 'Zend/Service/Exception.php'; throw new Zend_Service_Exception('Postmark returned ' . $response->getStatus() . ' - ' . $response->getMessage()); } return json_decode($response->getBody()); }