本文整理汇总了PHP中Psr\Http\Message\ResponseInterface::getStatusCode方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::getStatusCode方法的具体用法?PHP ResponseInterface::getStatusCode怎么用?PHP ResponseInterface::getStatusCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::getStatusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create
/**
* Factory method to create a new exception with a normalized error message
*
* @param RequestInterface $request Request
* @param ResponseInterface $response Response received
* @param \Exception $previous Previous exception
* @param array $ctx Optional handler context.
*
* @return self
*/
public static function create(RequestInterface $request, ResponseInterface $response = null, \Exception $previous = null, array $ctx = [])
{
if (!$response) {
return new self('Error completing request', $request, null, $previous, $ctx);
}
$level = (int) floor($response->getStatusCode() / 100);
if ($level === 4) {
$label = 'Client error';
$className = __NAMESPACE__ . '\\ClientException';
} elseif ($level === 5) {
$label = 'Server error';
$className = __NAMESPACE__ . '\\ServerException';
} else {
$label = 'Unsuccessful request';
$className = __CLASS__;
}
$uri = $request->getUri();
$uri = static::obfuscateUri($uri);
// Server Error: `GET /` resulted in a `404 Not Found` response:
// <html> ... (truncated)
$message = sprintf('%s: `%s` resulted in a `%s` response', $label, $request->getMethod() . ' ' . $uri, $response->getStatusCode() . ' ' . $response->getReasonPhrase());
$summary = static::getResponseBodySummary($response);
if ($summary !== null) {
$message .= ":\n{$summary}\n";
}
return new $className($message, $request, $response, $previous, $ctx);
}
示例2: getLastResponseStatus
/**
* @return string|null
*/
public function getLastResponseStatus()
{
if (is_null($this->lastResponse)) {
return null;
}
return $this->lastResponse->getStatusCode();
}
示例3: checkResponse
protected function checkResponse(ResponseInterface $response, $data)
{
if ($response->getStatusCode() >= 400) {
$msg = $data['error_description'] ?: (string) $response->getReasonPhrase();
throw new IdentityProviderException($msg, $response->getStatusCode(), $response);
}
}
示例4: checkResponse
/**
* Checks a provider response for errors.
*
* @throws IdentityProviderException
*
* @param ResponseInterface $response
* @param array|string $data Parsed response data
*
* @return void
*/
protected function checkResponse(ResponseInterface $response, $data)
{
$code = $response->getStatusCode();
if ($code > 400) {
throw new IdentityProviderException(isset($data['error']) ? $data['error'] : $response->getReasonPhrase(), $response->getStatusCode(), $response->getBody());
}
}
示例5: getStatusCode
/**
* Get status code of response.
*
* @return int
* @throws ComicApiException
*/
public function getStatusCode()
{
if ($this->response instanceof ResponseInterface) {
return $this->response->getStatusCode();
}
throw new ComicApiException("You can't get response without making request.");
}
示例6: checkResponse
protected function checkResponse(ResponseInterface $response, $data)
{
if ($response->getStatusCode() != 200) {
$data = json_decode($data, true);
throw new IdentityProviderException($data['message'], $response->getStatusCode(), $data);
}
}
示例7: __invoke
public function __invoke(ResponseInterface $response)
{
$level = $response->getStatusCode() >= 400 ? LogLevel::ERROR : LogLevel::INFO;
$message = "Request returned " . $response->getStatusCode() . " - " . $response->getReasonPhrase();
$this->logger->{$level}($message);
return $response;
}
示例8: _getResult
/**
* @param ResponseInterface $response
*
* @return ApiResult
*/
protected function _getResult($response)
{
if (!$response instanceof ResponseInterface) {
throw new \InvalidArgumentException("{$response} should be an instance of ResponseInterface");
}
$result = new ApiResult();
$result->setStatusCode($response->getStatusCode());
$callId = $response->getHeader('X-Call-Id');
if (!empty($callId)) {
$result->setCallId($callId);
}
$decoded = json_decode((string) $response->getBody());
if (isset($decoded->meta) && isset($decoded->data) && isset($decoded->meta->code) && $decoded->meta->code == $response->getStatusCode()) {
$meta = $decoded->meta;
$data = $decoded->data;
if (isset($meta->message)) {
$result->setStatusMessage($meta->message);
}
$result->setContent(json_encode($data));
} else {
$result->setContent((string) $response->getBody());
}
$result->setHeaders($response->getHeaders());
return $result;
}
示例9: checkResponse
/**
* Check a provider response for errors.
*
* @link https://developer.uber.com/v1/api-reference/
* @throws IdentityProviderException
* @param ResponseInterface $response
* @param string $data Parsed response data
* @return void
*/
protected function checkResponse(ResponseInterface $response, $data)
{
$acceptableStatuses = [200, 201];
if (!in_array($response->getStatusCode(), $acceptableStatuses)) {
throw new IdentityProviderException($data['message'] ?: $response->getReasonPhrase(), $response->getStatusCode(), $response);
}
}
示例10: response
private function response(ResponseInterface $response)
{
if ($response->getStatusCode() !== 200) {
throw new EventbriteResponseException($response->getStatusCode(), $response->getReasonPhrase());
}
return $response->getBody()->getContents();
}
示例11: checkResponse
protected function checkResponse(ResponseInterface $response, $data)
{
if ($response->getStatusCode() != 200) {
$data = is_array($data) ? $data : json_decode($data, true);
throw new IdentityProviderException($data['error_description'], $response->getStatusCode(), $data);
}
}
示例12: errorCode
/**
* Returns the HTTP status code of the error (or false if the HTTP status code is less than 399)
* @return boolean|int The HTTP status code returned from the API.
*/
public function errorCode()
{
if ($this->isError()) {
return (int) $this->response->getStatusCode();
}
return false;
}
示例13: processResourceRequestResponse
/**
* Checks request response and dispatch result to appropriate handler.
*
* @param ResponseInterface $request
*
* @return \Exception|mixed
*/
public function processResourceRequestResponse(ResponseInterface $request)
{
$response = json_decode($request->getBody()->getContents());
if (Response::HTTP_OK !== $request->getStatusCode() && Response::HTTP_CREATED !== $request->getStatusCode()) {
return ExceptionHandler::handle(get_class($this), $response, $request->getStatusCode());
}
return isset($response->data) ? json_decode(json_encode($response->data), true) : json_decode(json_encode($response), true);
}
示例14: throwApiExceptionWhenStatusIsUnexpected
/**
* @param array $allowedStatusCodes
* @param ResponseInterface $response
* @param string $errorMessage
* @throws \HostedSolr\ApiClient\Exception\HostedSolrApiException
*/
protected function throwApiExceptionWhenStatusIsUnexpected(array $allowedStatusCodes, $response, $errorMessage)
{
if (!in_array($response->getStatusCode(), $allowedStatusCodes)) {
$exception = new HostedSolrApiException($errorMessage . ' Statuscode: ' . (int) $response->getStatusCode());
$exception->setResponse($response);
throw $exception;
}
}
示例15: checkResponse
/**
* {@inheritDoc}
*/
protected function checkResponse(ResponseInterface $response, $data)
{
if (isset($data['error'])) {
if (is_array($data['error'])) {
throw new IdentityProviderException($data['error']['messageKey'] . ': ' . $data['error']['message'] ?: $response->getReasonPhrase(), $data['error']['code'] ?: $response->getStatusCode(), $response);
}
throw new IdentityProviderException($data['error_description'] ?: $response->getReasonPhrase(), $data['error_code'] ?: $response->getStatusCode(), $response);
}
}