本文整理汇总了PHP中Guzzle\Http\Message\Response::getStatusCode方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::getStatusCode方法的具体用法?PHP Response::getStatusCode怎么用?PHP Response::getStatusCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\Response
的用法示例。
在下文中一共展示了Response::getStatusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: request
/**
* Returns object with properties int:status object:body
* @param string $method
* @param string $path
* @param array $query
* @param bool $doAuth
* @throws \InvalidArgumentException
* @throws \Exception
* @return \stdClass
*/
public function request($method, $path, $query = array(), $doAuth = false)
{
$this->userAgent = 'Rocker REST Client v' . Server::VERSION;
$method = strtolower($method);
$request = $this->initiateRequest($method, $path, $query);
if ($doAuth) {
$this->addAuthHeader($request);
}
try {
$this->lastResponse = $request->send();
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
$this->lastResponse = $e->getResponse();
if ($this->lastResponse->getStatusCode() == 401 && !$doAuth && !empty($this->user)) {
trigger_error('Doing unauthenticated requests to an URI that requires authentication (' . $path . ')', E_WARNING);
return $this->request($method, $path, $query, true);
}
}
if ($this->lastResponse->getStatusCode() == 400) {
throw new ClientException($this->lastResponse, 400);
}
if ($this->lastResponse->getStatusCode() == 204) {
return (object) array('status' => 204, 'body' => array());
}
if (strpos($this->lastResponse->getContentType(), 'json') === false) {
throw new ClientException($this->lastResponse, ClientException::ERR_UNEXPECTED_CONTENT_TYPE, 'Server responded with unexpected content type (' . $this->lastResponse->getContentType() . ')');
}
$str = (string) $this->lastResponse->getBody();
$body = json_decode($str);
return (object) array('status' => $this->lastResponse->getStatusCode(), 'headers' => $this->headerCollectionToArray($this->lastResponse->getHeaders()), 'body' => $body);
}
示例2: createMessage
/**
* @param Response $response
*
* @return string
*/
protected static function createMessage(Response $response)
{
if ($response->getStatusCode() != 400) {
return '[' . $response->getStatusCode() . '] A HTTP error has occurred: ' . $response->getBody(true);
}
$message = 'Some errors occurred:';
foreach ($response->xml()->error as $error) {
$message .= PHP_EOL . '[' . (string) $error->code . '] ' . (string) $error->message;
}
return $message;
}
示例3: factory
/**
* Simple exception factory for creating Intercom standardised exceptions
*
* @param RequestInterface $request The Request
* @param Response $response The response
* @return BadResponseException
*/
public static function factory(RequestInterface $request, Response $response)
{
$response_json = $response->json();
$intercom_unavailable_error = NULL;
if (!static::isValidIntercomError($response_json)) {
if ($response->isServerError()) {
$label = 'Server error response';
$class = __NAMESPACE__ . '\\ServerErrorResponseException';
$intercom_unavailable_error = 'Service Unavailable: Back-end server is at capacity';
} else {
$label = 'Unsuccessful response';
$class = __CLASS__;
}
} elseif ($response->isClientError()) {
$label = 'Client error response';
$class = __NAMESPACE__ . '\\ClientErrorResponseException';
} elseif ($response->isServerError()) {
$label = 'Server error response';
$class = __NAMESPACE__ . '\\ServerErrorResponseException';
} else {
$label = 'Unsuccessful response';
$class = __CLASS__;
}
$message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl()));
$e = new $class($message);
$e->setResponse($response);
$e->setRequest($request);
// Sets the errors if the error response is the standard Intercom error type
if (static::isValidIntercomError($response_json)) {
$e->setErrors($response_json['errors']);
} elseif ($intercom_unavailable_error != NULL) {
$e->setErrors(array('code' => 'service_unavailable', "message" => $intercom_unavailable_error));
}
return $e;
}
示例4: handleError
/**
* @throws HttpException
*/
protected function handleError()
{
$body = (string) $this->response->getBody(true);
$code = (int) $this->response->getStatusCode();
$content = json_decode($body);
throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
}
示例5: factory
/**
* Simple exception factory for creating Intercom standardised exceptions
*
* @param RequestInterface $request The Request
* @param Response $response The response
* @return BadResponseException
*/
public static function factory(RequestInterface $request, Response $response)
{
if (!static::isValidIntercomError($response->json())) {
$label = 'Unsuccessful response';
$class = __CLASS__;
} elseif ($response->isClientError()) {
$label = 'Client error response';
$class = __NAMESPACE__ . '\\ClientErrorResponseException';
} elseif ($response->isServerError()) {
$label = 'Server error response';
$class = __NAMESPACE__ . '\\ServerErrorResponseException';
} else {
$label = 'Unsuccessful response';
$class = __CLASS__;
}
$message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl()));
$e = new $class($message);
$e->setResponse($response);
$e->setRequest($request);
// Sets the errors if the error response is the standard Intercom error type
if (static::isValidIntercomError($response->json())) {
$e->setErrors($response->json()['errors']);
}
return $e;
}
示例6: determineCode
/**
* Determine an exception code from the given response, exception data and
* JSON body.
*
* @param Response $response
* @param array $data
* @param array $json
*
* @return string|null
*/
private function determineCode(Response $response, array $data, array $json)
{
if (409 === $response->getStatusCode()) {
return 'DocumentAlreadyExists';
}
return $this->determineCodeFromErrors($data['errors']);
}
示例7: validateResponse
/**
* Validate the HTTP response and throw exceptions on errors
*
* @throws ServerException
*/
private function validateResponse()
{
if ($this->httpResponse->getStatusCode() !== 200) {
$statusCode = $this->httpResponse->getStatusCode();
throw new ServerException('Server responded with HTTP status ' . $statusCode, $statusCode);
} else {
if (strpos($this->httpResponse->getHeader('Content-Type'), 'application/json') === false) {
throw new ServerException('Server did not respond with the expected content-type (application/json)');
}
}
try {
$this->httpResponse->json();
} catch (RuntimeException $e) {
throw new ServerException($e->getMessage());
}
}
示例8: onRequestError
/**
* request error
* @param \Guzzle\Common\Event $event
*/
public function onRequestError(Event $event)
{
$this->request = $event->offsetGet('request');
$this->response = $event->offsetGet('response');
$body = $this->response->getBody(true);
switch ($this->response->getStatusCode()) {
case 400:
$this->error400($body);
break;
case 520:
$this->error520($body);
break;
default:
$this->commonError($body);
break;
}
}
示例9: factory
/**
* Factory method to create a new Oauth exception.
*
* @param RequestInterface $request
* @param Response $response
*
* @return OauthException
*/
public static function factory(RequestInterface $request, Response $response)
{
$message = 'Client error response' . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl()));
$e = new static($message);
$e->setResponse($response);
$e->setRequest($request);
return $e;
}
示例10: isSuccess
/**
* Should return if sending the data was successful
*
* @return bool
*/
public function isSuccess()
{
$statuscode = $this->response->getStatusCode();
if (!in_array($statuscode, ['200', '204'])) {
throw new \Exception('HTTP Code ' . $statuscode . ' ' . $this->response->getBody());
}
return true;
}
示例11: fromCommand
/**
* {@inheritDoc}
*/
public static function fromCommand(CommandInterface $command, Response $response)
{
$errors = json_decode($response->getBody(true), true);
$type = array_get($errors, 'error.type', null);
$code = array_get($errors, 'error.code', null);
$message = array_get($errors, 'error.message', null);
$class = '\\Apache\\Usergrid\\Api\\Exception\\' . studly_case($type) . 'Exception';
if (class_exists($class)) {
$exception = new $class($message, $response->getStatusCode());
} else {
$exception = new static($message, $response->getStatusCode());
}
$exception->setErrorType($type);
$exception->setResponse($response);
$exception->setRequest($command->getRequest());
return $exception;
}
示例12: parseHeaders
/**
* Parses additional exception information from the response headers
*
* @param RequestInterface $request Request that was issued
* @param Response $response The response from the request
* @param array $data The current set of exception data
*/
protected function parseHeaders(RequestInterface $request, Response $response, array &$data)
{
$data['message'] = $response->getStatusCode() . ' ' . $response->getReasonPhrase();
if ($requestId = $response->getHeader('x-amz-request-id')) {
$data['request_id'] = $requestId;
$data['message'] .= " (Request-ID: {$requestId})";
}
}
示例13: fromCommand
/**
* {@inheritDoc}
*/
public static function fromCommand(CommandInterface $command, Response $response)
{
$errors = json_decode($response->getBody(true), true);
$type = $errors['error']['type'];
$message = isset($errors['error']['message']) ? $errors['error']['message'] : 'Unknown error';
$code = isset($errors['error']['code']) ? $errors['error']['code'] : null;
// We can trigger very specific exception based on the type and code
if ($type === 'card_error') {
$exception = new CardErrorException($message, $response->getStatusCode());
} elseif ($type === 'invalid_request_error' && $code === 'rate_limit') {
$exception = new ApiRateLimitException($message, $response->getStatusCode());
} else {
$exception = new static($message, $response->getStatusCode());
}
$exception->setRequest($command->getRequest());
$exception->setResponse($response);
return $exception;
}
示例14: prepareResponse
protected function prepareResponse(Response $data)
{
if ($data->getStatusCode() != 200) {
throw new \InvalidArgumentException($data->getReasonPhrase());
}
$response = json_decode($data->getBody(), true);
$this->validateResponse($response);
return $response;
}
示例15: validate
/**
* @param $statusCode
* @param $url
* @param RequestInterface $request
* @param Response $response
*/
public function validate($statusCode, $url, RequestInterface $request, Response $response)
{
if ($response->getStatusCode() === $statusCode) {
return;
}
$message = $url . ' gives a non-200 status code response.';
$this->logger->warning($message, array('request' => (string) $request, 'response' => (string) $response));
throw new RuntimeException($message . ' See logs.' . $response->serialize());
}