本文整理汇总了PHP中Psr\Http\Message\ResponseInterface::getBody方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::getBody方法的具体用法?PHP ResponseInterface::getBody怎么用?PHP ResponseInterface::getBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::getBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: forUnexpectedContent
public static function forUnexpectedContent(ResponseInterface $response, $expected)
{
$response->getBody()->rewind();
$ex = new self(sprintf('Unexpected content in reponse. Expected a %s got %s', $expected, $response->getBody()->getContents()));
$ex->responseObject = $response;
return $ex;
}
示例2: _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;
}
示例3: decode
public static function decode(ResponseInterface $response)
{
if ($response->hasHeader('Content-Type') && $response->getHeader('Content-Type')[0] == 'application/json') {
return json_decode((string) $response->getBody(), true);
}
return (string) $response->getBody();
}
示例4: convertIncomingResponseToArray
/**
* Turns an HTTP response object, with JSON in the body, into an array
*/
public function convertIncomingResponseToArray(ResponseInterface $response) : array
{
$response->getBody()->rewind();
$body = $response->getBody()->getContents();
$bodyArray = json_decode($body, true);
return $bodyArray ?: [];
}
示例5: getLastResponseData
/**
* @return string|null
*/
public function getLastResponseData()
{
if (is_null($this->lastResponse)) {
return null;
}
return $this->lastResponse->getBody()->getContents();
}
示例6: __invoke
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
{
$accessToken = Helper::getTokenFromReq($request);
if ($accessToken == null) {
$res['ret'] = 0;
$res['msg'] = "token is null";
$response->getBody()->write(json_encode($res));
return $response;
}
$storage = Factory::createTokenStorage();
$token = $storage->get($accessToken);
if ($token == null) {
$res['ret'] = 0;
$res['msg'] = "token is null";
$response->getBody()->write(json_encode($res));
return $response;
}
if ($token->expireTime < time()) {
$res['ret'] = 0;
$res['msg'] = "token is expire";
$response->getBody()->write(json_encode($res));
return $response;
}
$response = $next($request, $response);
return $response;
}
示例7: getResponse
/**
* Get content of response.
*
* @param bool $parseJsonToObject Parse JSON response to PHP object?
*
* @return string|object
* @throws ComicApiException
*/
public function getResponse($parseJsonToObject = false)
{
if ($this->response instanceof ResponseInterface) {
return $this->response->getBody();
}
throw new ComicApiException("You can't get response without making request.");
}
示例8: display
/**
* Outputs the error popup, or a plain message, depending on the response content type.
*
* @param \Exception|\Error $exception Note: can't be type hinted, for PHP7 compat.
* @param ResponseInterface|null $response If null, it outputs directly to the client. Otherwise, it assumes the
* object is a new blank response.
* @return ResponseInterface|null
*/
static function display($exception, ResponseInterface $response = null)
{
// For HTML pages, output the error popup
if (strpos(get($_SERVER, 'HTTP_ACCEPT'), 'text/html') !== false) {
ob_start();
ErrorConsoleRenderer::renderStyles();
$stackTrace = self::getStackTrace($exception->getPrevious() ?: $exception);
ErrorConsoleRenderer::renderPopup($exception, self::$appName, $stackTrace);
$popup = ob_get_clean();
// PSR-7 output
if ($response) {
$response->getBody()->write($popup);
return $response->withStatus(500);
}
// Direct output
echo $popup;
} else {
// PSR-7 output
if ($response) {
$response->getBody()->write($exception->getMessage());
if (self::$devEnv) {
$response->getBody()->write("\n\nStack trace:\n" . $exception->getTraceAsString());
}
return $response->withoutHeader('Content-Type')->withHeader('Content-Type', 'text-plain')->withStatus(500);
}
// Direct output
header("Content-Type: text/plain");
http_response_code(500);
echo $exception->getMessage();
if (self::$devEnv) {
echo "\n\nStack trace:\n" . $exception->getTraceAsString();
}
}
return null;
}
示例9: dispatch
public function dispatch(ServerRequestInterface $request, ResponseInterface $response) : ResponseInterface
{
$lang = LanguageNegotiator::getLanguage($request);
$query = $this->db->prepare('SELECT n.id, n.title, n.parent_id from nodes n
INNER JOIN node_attributes na ON na.node_id = n.id
INNER JOIN attribute_values av on na.`attribute_value_id` = av.`id`
where av.`attribute_value` = :language;');
$query->bindParam(':language', $lang);
$query->execute();
$data = $query->fetchAll(\PDO::FETCH_ASSOC);
$neg = new FormatNegotiator();
$contentType = $neg->getFormat($request);
switch ($contentType) {
case 'xml':
$xml = new \SimpleXMLElement('<root/>');
array_walk_recursive($this->treeBuilder->build($data), array($xml, 'addChild'));
$response->getBody()->write($xml->asXML());
break;
default:
$response->getBody()->write(json_encode($this->treeBuilder->build($data)));
break;
}
return $response->withoutHeader('Content-Type');
// Remove the Content-Type Header
}
示例10: parseStream
/**
* @param callable $fn
*/
public function parseStream(callable $fn)
{
$body = $this->response->getBody();
while (!$body->eof()) {
$data = $this->parseEventData($body->read(1024));
$fn($data);
}
}
示例11: getResponse
public function getResponse()
{
if ($this->response && ($body = $this->response->getBody())) {
// Rewind response body in case it has already been read
$body->seek(0, SEEK_SET);
}
return $this->response;
}
示例12: asArray
/**
* Retrieve the json encoded response as a php associative array
*
* @return array
*/
public function asArray()
{
$stream = $this->response->getBody();
if ($stream->eof()) {
$stream->rewind();
}
return json_decode($stream->getContents(), true);
}
示例13: createErrorDocument
/**
* @return \WoohooLabs\Yin\JsonApi\Document\AbstractErrorDocument
*/
protected function createErrorDocument()
{
$errorDocument = new ErrorDocument();
if ($this->includeOriginalBody === true) {
$errorDocument->setMeta(["original" => json_decode($this->response->getBody(), true)]);
}
return $errorDocument;
}
示例14: to
/**
* Return response with JSON header and status.
*
* @param \Psr\Http\Message\ResponseInterface $response
* @param int $status
* @return mixed
*/
public function to(ResponseInterface $response, $status = 200)
{
if ($this->isRender) {
$response->getBody()->write($this->jsonEncode($this->data, $this->encodingOptions));
} else {
$response->getBody()->write($this->data);
}
return $response->withStatus($status)->withHeader('Content-Type', 'application/json;charset=utf-8');
}
示例15: parseResponse
/**
* @param ResponseInterface $response
* @return array
* @throws SurvosException
*/
protected function parseResponse($response)
{
$content = $response->getBody()->getContents();
$data = json_decode($content, true);
if (!is_array($data)) {
throw new SurvosException("Bad data in server response: " . substr($response->getBody(), 0, 200));
}
return $data;
}