/** * Handle all API errors. */ public function errorAction() { $data = array(); $exception = $this->_getParam('error_handler')->exception; // Add code and message to response. $code = $exception->getCode(); if (!$code) { $code = self::DEFAULT_HTTP_RESPONSE_CODE; } $message = $exception->getMessage(); if (!$message) { $message = Zend_Http_Response::responseCodeAsText($code); } $data['message'] = $message; // Add errors to response. if ($exception instanceof Omeka_Controller_Exception_Api) { if ($errors = $exception->getErrors()) { $data['errors'] = $errors; } } try { $this->getResponse()->setHttpResponseCode($code); } catch (Zend_Controller_Exception $e) { // The response code was invalid. Set the default. $this->getResponse()->setHttpResponseCode(self::DEFAULT_HTTP_RESPONSE_CODE); } $this->_helper->json($data); }
/** * Make a HTTP request. * @param string $method * @param array $getParameters * @param array $postParameters * @throws Insulin_Service_Rest_ConnectionFailedException * @throws Insulin_Service_Rest_HttpErrorException */ protected function _doRequest($method, array $getParameters = null, array $postParameters = null) { $link = $this->getLink(); if (!empty($postParameters)) { $link->setParameterPost($postParameters); } if (!empty($getParameters)) { $link->setParameterGet($getParameters); } $link->setUri($this->_endpoint); $link->setMethod($method); require_once 'Zend/Http/Exception.php'; try { $response = $link->request(); } catch (Zend_Http_Exception $e) { require_once 'Insulin/Service/Rest/ConnectionFailedException.php'; throw new Insulin_Service_Rest_ConnectionFailedException(); } if ($response->isError()) { require_once 'Insulin/Service/Rest/HttpErrorException.php'; // extract error code from response $code = Zend_Http_Response::extractCode($response); // extract standard error message from response $msg = Zend_Http_Response::responseCodeAsText($code); throw new Insulin_Service_Rest_HttpErrorException($code, $msg); } return $response; }
public function __construct($statusCode, $isFlexClient = false) { $this->_isFlexClient = $isFlexClient; $this->_realStatusCode = $statusCode; if ($this->_isFlexClient == true) { $this->_statusCode = 200; $this->_statusMessage = Zend_Http_Response::responseCodeAsText($this->_realStatusCode); parent::__construct(200, $_SERVER); header("HTTP/1.1 200 OK"); } else { $this->_statusCode = $statusCode; $this->_statusMessage = Zend_Http_Response::responseCodeAsText($statusCode); parent::__construct($statusCode, $_SERVER); header("HTTP/1.1 {$statusCode} " . $this->_statusMessage); } }
public function testUnknownCode() { $response_str = $this->readResponse('response_unknown'); $response = Zend_Http_Response::fromString($response_str); // Check that dynamically the message is parsed $this->assertEquals(550, $response->getStatus(), 'Status is expected to be a non-standard 550'); $this->assertEquals('Printer On Fire', $response->getMessage(), 'Message is expected to be extracted'); // Check that statically, an Unknown string is returned for the 550 code $this->assertEquals('Unknown', Zend_Http_Response::responseCodeAsText($response_str)); }
/** Send a request to the server. * * @param string $method * @param string $path * @param array $params * * @return string server response. */ public function fetch($method, $path, array $params = array()) { $key = $this->_genContentKey($path, $params); if (isset($this->_content[$method][$key])) { list($content, $status) = $this->_content[$method][$key]; } elseif (isset($this->_content[self::METHOD_ANY][$key])) { list($content, $status) = $this->_content[self::METHOD_ANY][$key]; } else { $status = self::STATUS_NOT_FOUND; $content = Zend_Http_Response::responseCodeAsText(self::STATUS_NOT_FOUND); } $this->_requests[] = $key; return new JsonApi_Http_Response($this->getUri($path, $params, $method), $status, $content); }
/** * Maps HTTP status code to correct HTTP response text. */ public function responseCodeAsText() { require_once 'Zend/Http/Response.php'; return Zend_Http_Response::responseCodeAsText($this->code); }
/** * Applies the search filter and makes the actual API request * * @param array $searchFilter * @return string */ private function _apiRequest(array $searchFilter) { $this->_setSearchFilter($searchFilter); $serviceUri = $this->_apiEndpoint . ".{$this->_responseFormat}"; $client = new Zend_Http_Client($serviceUri); $client->setParameterGet(array('key' => $this->_apiKey)); foreach ($this->_searchFilter as $key => $value) { if ($key === 'url') { $key = 'q'; } $client->setParameterGet(array($key => $value)); } $response = $client->request(); if (403 === Zend_Http_Response::extractCode($response)) { $codeAsText = strtolower(Zend_Http_Response::responseCodeAsText(403)); throw new Exception("Access to Backtweets API {$codeAsText}, check used API key"); } return $response->getBody(); }
/** Appends relevant error message information to a failed status check. * * @param Test_Browser $browser * * @return string */ protected function failureDescription($browser) { $code = $browser->getResponse()->getStatusCode(); /* See if there's an error we can report. */ if (!($error = (string) $browser->getError())) { $error = Zend_Http_Response::responseCodeAsText($code); } return sprintf(self::MESSAGE, $this->toString(), $code, $error); }
/** Accessor for $_status. * * @param bool $asString * * @return int */ public function getStatus($asString = false) { return $asString ? Zend_Http_Response::responseCodeAsText($this->_status) : $this->_status; }
/** * 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; }
public function __construct($responseCode, $message = null) { parent::__construct(null !== $message ? $message : Zend_Http_Response::responseCodeAsText($responseCode), self::ERROR_CODE_BASE + (int) $responseCode); $this->_responseCode = $responseCode; }
/** * Send the $exception's code or $code as the formal HTTP status of the response. * * @param integer $code suggested HTTP response code, if exception fails to provide one * @param Exception $exception OPTIONAL HTTP status code * @return string status code and description */ private static function setHeaderStatus($code, Exception $exception = null) { switch ($code) { case 404: $status = '404 Not Found'; break; case 500: $status = '500 Internal Server Error'; break; default: require_once 'Zend/Http/Response.php'; $status = $code . ' ' . Zend_Http_Response::responseCodeAsText($code); } if ('cgi' !== substr(php_sapi_name(), 0, 3) && !empty($_SERVER['SERVER_PROTOCOL'])) { header($_SERVER['SERVER_PROTOCOL'] . ' ' . $status, true); } else { header('Status: ' . $status, true); } return $status; }