本文整理汇总了PHP中Psr\Http\Message\ResponseInterface类的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface类的具体用法?PHP ResponseInterface怎么用?PHP ResponseInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ResponseInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: checkResponseStatusCode
/**
* Check the response status code.
*
* @param ResponseInterface $response
* @param int $expectedStatusCode
*
* @throws \RuntimeException on unexpected status code
*/
private function checkResponseStatusCode(ResponseInterface $response, $expectedStatusCode)
{
$statusCode = $response->getStatusCode();
if ($statusCode !== $expectedStatusCode) {
throw new \RuntimeException('Wunderlist API returned status code ' . $statusCode . ' expected ' . $expectedStatusCode);
}
}
示例2: mainAction
/**
* @param ServerRequestInterface $request the current request
* @param ResponseInterface $response
* @return ResponseInterface the response with the content
*/
public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
{
$this->initPage();
$this->main();
$response->getBody()->write($this->content);
return $response;
}
示例3: checkResponse
/**
* Checks Fitbit API response for errors.
*
* @throws IdentityProviderException
* @param ResponseInterface $response
* @param array|string $data Parsed response data
* @return void
*/
protected function checkResponse(ResponseInterface $response, $data)
{
if (!empty($data['errors'][0])) {
$message = $data['errors'][0]['errorType'] . ': ' . $data['errors'][0]['message'];
throw new IdentityProviderException($message, $response->getStatusCode(), $data);
}
}
示例4: __invoke
/**
* Invoke error handler
*
* @param ServerRequestInterface $request The most recent Request object
* @param ResponseInterface $response The most recent Response object
* @param string[] $methods Allowed HTTP methods
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
{
if ($request->getMethod() === 'OPTIONS') {
$status = 200;
$contentType = 'text/plain';
$output = $this->renderPlainNotAllowedMessage($methods);
} else {
$status = 405;
$contentType = $this->determineContentType($request);
switch ($contentType) {
case 'application/json':
$output = $this->renderJsonNotAllowedMessage($methods);
break;
case 'text/xml':
case 'application/xml':
$output = $this->renderXmlNotAllowedMessage($methods);
break;
case 'text/html':
$output = $this->renderHtmlNotAllowedMessage($methods);
break;
}
}
$body = new Body(fopen('php://temp', 'r+'));
$body->write($output);
$allow = implode(', ', $methods);
return $response->withStatus($status)->withHeader('Content-type', $contentType)->withHeader('Allow', $allow)->withBody($body);
}
示例5: payload
protected function payload(ResponseInterface $response, StructureShape $member, array &$result)
{
$jsonBody = $this->parseJson($response->getBody());
if ($jsonBody) {
$result += $this->parser->parse($member, $jsonBody);
}
}
示例6: validateBody
/**
* @param \Psr\Http\Message\ResponseInterface $response
* @throws \Exception
*/
public function validateBody(ResponseInterface $response)
{
$errors = $this->validateMessage(json_decode($response->getBody()));
if (empty($errors) === false) {
$this->exceptionFactory->createResponseBodyInvalidJsonApiException($response, $errors, $this->includeOriginalMessage);
}
}
示例7: getNotFoundResponse
protected function getNotFoundResponse(RequestInterface $request, ResponseInterface $response, \Exception $exception)
{
$output = json_encode(['error_message' => $exception->getMessage()], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$body = new Body(fopen('php://temp', 'r+'));
$body->write($output);
return $response->withStatus(404)->withHeader('Content-type', 'application/json')->withBody($body);
}
示例8: create
/**
* Return a new exception depending on the HTTP status code
*
* @param ResponseInterface $response Server-side response
* @return ActionAlreadyDoneException|BadRequestException|BadTokenException|PermissionDeniedException|RealDebridException|UnknownResourceException
*/
public static function create(ResponseInterface $response)
{
$error = json_decode($response->getBody());
switch ($response->getStatusCode()) {
case 202:
return new ActionAlreadyDoneException();
break;
/*case 400:
return new BadRequestException();
break;*/
/*case 400:
return new BadRequestException();
break;*/
case 401:
return new BadTokenException();
break;
case 403:
return new PermissionDeniedException();
break;
case 404:
return new UnknownResourceException();
break;
case 503:
return new FileUnavailableException();
break;
}
return new RealDebridException($error);
}
示例9: 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);
}
}
示例10: httpError
/**
* Helper method responsible for constructing and returning {@see BadResponseError} exceptions.
*
* @param RequestInterface $request The faulty request
* @param ResponseInterface $response The error-filled response
*
* @return BadResponseError
*/
public function httpError(RequestInterface $request, ResponseInterface $response)
{
$message = $this->header('HTTP Error');
$message .= sprintf("The remote server returned a \"%d %s\" error for the following transaction:\n\n", $response->getStatusCode(), $response->getReasonPhrase());
$message .= $this->header('Request');
$message .= trim(str($request)) . PHP_EOL . PHP_EOL;
$message .= $this->header('Response');
$message .= trim(str($response)) . PHP_EOL . PHP_EOL;
$message .= $this->header('Further information');
// @codeCoverageIgnoreStart
switch ($response->getStatusCode()) {
case 400:
$message .= "Please ensure that your input values are valid and well-formed. ";
break;
case 401:
$message .= "Please ensure that your authentication credentials are valid. ";
break;
case 404:
$message .= "Please ensure that the resource you're trying to access actually exists. ";
break;
case 500:
$message .= "Please try this operation again once you know the remote server is operational. ";
break;
}
// @codeCoverageIgnoreEnd
$message .= "Visit http://docs.php-opencloud.com/en/latest/http-codes for more information about debugging " . "HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues.";
$e = new BadResponseError($message);
$e->setRequest($request);
$e->setResponse($response);
return $e;
}
示例11: 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.");
}
示例12: modify
public function modify(ResponseInterface $response) : ResponseInterface
{
if (false === strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
return $response;
}
return $response->withHeader('Content-Type', $this->contentType);
}
示例13: handleError
private function handleError(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
{
$output = $this->renderJsonErrorMessage($exception);
$body = new Body(fopen('php://temp', 'r+'));
$body->write($output);
return $response->withStatus(500)->withHeader('Content-type', 'application/json')->withBody($body);
}
示例14: checkResponse
/**
* Check a provider response for errors.
*
* @throws IdentityProviderException
* @param ResponseInterface $response
* @param string $data Parsed response data
* @return void
*/
protected function checkResponse(ResponseInterface $response, $data)
{
$statusCode = $response->getStatusCode();
if ($statusCode >= 400) {
throw new IdentityProviderException(isset($data['meta']['errorDetail']) ? $data['meta']['errorDetail'] : $response->getReasonPhrase(), $statusCode, $response);
}
}
示例15: fileAction
/**
* Returns the JS file content
*
* @param ContainerInterface $container Dependency injection container
* @param ServerRequestInterface $request Request object
* @param ResponseInterface $response Response object
* @param array $args Associative list of route parameters
* @return ResponseInterface $response Modified response object with generated output
*/
public static function fileAction(ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args)
{
$contents = '';
$files = array();
$aimeos = $container->get('aimeos');
$type = isset($args['type']) ? $args['type'] : 'js';
foreach ($aimeos->getCustomPaths('admin/jqadm') as $base => $paths) {
foreach ($paths as $path) {
$jsbAbsPath = $base . '/' . $path;
$jsb2 = new \Aimeos\MW\Jsb2\Standard($jsbAbsPath, dirname($jsbAbsPath));
$files = array_merge($files, $jsb2->getFiles($type));
}
}
foreach ($files as $file) {
if (($content = file_get_contents($file)) !== false) {
$contents .= $content;
}
}
$response->getBody()->write($contents);
if ($type === 'js') {
$response = $response->withHeader('Content-Type', 'application/javascript');
} elseif ($type === 'css') {
$response = $response->withHeader('Content-Type', 'text/css');
}
return $response;
}