本文整理汇总了PHP中Psr\Http\Message\ServerRequestInterface::getBody方法的典型用法代码示例。如果您正苦于以下问题:PHP ServerRequestInterface::getBody方法的具体用法?PHP ServerRequestInterface::getBody怎么用?PHP ServerRequestInterface::getBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ServerRequestInterface
的用法示例。
在下文中一共展示了ServerRequestInterface::getBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
public function execute(Request $req, Response $res, callable $next = null)
{
try {
$name = urldecode($req->getAttribute('name'));
$collection = $this->boot()->get('store')->getCollection($name);
$contenttype = $req->getHeader('Content-Type');
if (false !== strpos($contenttype[0], 'json')) {
$body = json_decode($req->getBody(), true);
} else {
$body = $req->getParsedBody();
}
if (!$body) {
throw new \RuntimeException('no request body');
}
$data = $collection->save($collection->item()->setData($body))->getData();
if (isset($body['@labels'])) {
foreach ($body['@labels'] as $label) {
$this->boot()->get('store')->setLabel($data['id'], $label['_label_name'], $label['_label_group'], true);
}
}
if (isset($body['@properties'])) {
foreach ($body['@properties'] as $property) {
$this->boot()->get('store')->setProperty($data['id'], $property['_property_name'], $property['_property_value'], $property['_property_idx'], $property['_property_type'], $property['_property_sys_type_id'], true);
}
}
$data['@labels'] = $this->boot()->get('store')->getLabels($data['id']);
$data['@properties'] = $this->boot()->get('store')->getProperties($data['id']);
return $next($req, new JsonResponse(['status' => 'ok', 'data' => $data]));
} catch (\Exception $ex) {
return new JsonResponse(['status' => 'error', 'error' => $ex->getMessage()], 500);
}
}
示例2: getParsedBody
/**
* @inheritDoc
*/
public function getParsedBody()
{
if ($this->serverRequest->getBody()->getContents() && empty($this->serverRequest->getParsedBody())) {
$this->serverRequest = $this->serverRequest->withParsedBody(json_decode($this->serverRequest->getBody(), true));
}
return $this->serverRequest->getParsedBody();
}
示例3: parseJsonBody
protected function parseJsonBody(ServerRequestInterface $request)
{
$bodyJson = @json_decode($request->getBody()->__toString(), true);
if (json_last_error() != JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
return $bodyJson;
}
示例4: createRequest
/**
* {@inheritdoc}
*/
public function createRequest(ServerRequestInterface $psrRequest)
{
$parsedBody = $psrRequest->getParsedBody();
$parsedBody = is_array($parsedBody) ? $parsedBody : array();
$request = new Request($psrRequest->getQueryParams(), $parsedBody, $psrRequest->getAttributes(), $psrRequest->getCookieParams(), $this->getFiles($psrRequest->getUploadedFiles()), $psrRequest->getServerParams(), $psrRequest->getBody()->__toString());
$request->headers->replace($psrRequest->getHeaders());
return $request;
}
示例5: send
/**
* @param ServerRequestInterface $request
*/
public function send(ServerRequestInterface $request)
{
$this->currentUri = $request->getUri();
$this->setBase($this->currentUri);
if (empty($this->currentUri->getHost())) {
$this->currentUri = new Uri($this->getBase() . $this->currentUri);
}
$curl = curl_init();
$options = [CURLOPT_URL => (string) $this->currentUri, CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => $request->getMethod(), CURLOPT_USERAGENT => Loader::USER_AGENT, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => $this->getConvertedHeaders($request->getHeaders())];
if ($request->getBody()->getSize()) {
$options[CURLOPT_POSTFIELDS] = (string) $request->getBody();
}
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$this->currentUri = new Uri(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL));
return \GuzzleHttp\Psr7\parse_response($response);
}
示例6: __invoke
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
if (str_contains($request->getHeaderLine('content-type'), 'json')) {
$input = json_decode($request->getBody(), true);
$request = $request->withParsedBody($input ?: []);
}
return $out ? $out($request, $response) : $response;
}
示例7: getCredentials
/**
* @inheritDoc
*/
public function getCredentials(ServerRequestInterface $request)
{
$json = json_decode($request->getBody()->getContents(), true);
if (empty($json[$this->identifier]) || empty($json[$this->password])) {
return null;
}
return new Credentials($json[$this->identifier], $json[$this->password]);
}
示例8: convertRequestFromPsr7
/**
* Converts a PSR-7 request into an OAuth2 request.
*
* @param ServerRequestInterface $psrRequest
* @return Request
*/
public static function convertRequestFromPsr7(ServerRequestInterface $psrRequest)
{
$headers = [];
foreach ($psrRequest->getHeaders() as $header => $value) {
$headers[$header] = implode(';', $value);
}
return new Request($psrRequest->getQueryParams(), is_array($psrRequest->getParsedBody()) ? $psrRequest->getParsedBody() : [], $psrRequest->getAttributes(), $psrRequest->getCookieParams(), self::getFiles($psrRequest->getUploadedFiles()), $psrRequest->getServerParams(), $psrRequest->getBody()->__toString(), $headers);
}
示例9: modify
public function modify(ServerRequestInterface $request) : ServerRequestInterface
{
if (false === strpos($request->getHeaderLine('Content-Type'), 'application/json')) {
return $request;
}
// TODO: Implement broken json handling
return $request->withParsedBody(json_decode($request->getBody()->getContents(), $this->asAssociativeArray));
}
示例10: modify
public function modify(ServerRequestInterface $request) : ServerRequestInterface
{
if (false === strpos($request->getHeaderLine('Content-Type'), 'application/x-www-form-urlencoded')) {
return $request;
}
parse_str($request->getBody()->getContents(), $parsedBody);
return $request->withParsedBody($parsedBody);
}
示例11: getBody
/**
* @return string
*/
private function getBody()
{
if (isset($this->body)) {
return $this->body;
}
$this->body = (string) $this->request->getBody();
return $this->body;
}
示例12: parse
/**
* {@inheritDoc}
*/
public function parse(ServerRequestInterface $request)
{
$rawBody = (string) $request->getBody();
$parsedBody = json_decode($rawBody, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new MalformedRequestBodyException('Error when parsing JSON request body: ' . json_last_error_msg());
}
return $request->withAttribute('rawBody', $rawBody)->withParsedBody($parsedBody);
}
示例13: createRequest
public static function createRequest(PsrServerRequestInterface $psrRequest)
{
$request = new Request(new Uri($psrRequest->getUri()), $psrRequest->getMethod(), $psrRequest->getHeaders(), $psrRequest->getBody());
$attributes = $psrRequest->getAttributes();
foreach ($attributes as $name => $value) {
$request->setAttribute($name, $value);
}
return $request;
}
示例14: __invoke
/**
* Parses request bodies based on content type
*
* @param Request $request
* @param Response $response
* @param callable $next
* @return Response
*/
public function __invoke(Request $request, Response $response, callable $next)
{
$mime = strtolower($request->getHeaderLine('Content-Type'));
if ($this->isApplicableMimeType($mime) && !$request->getParsedBody()) {
$parsed = $this->getParsedBody((string) $request->getBody());
$request = $request->withParsedBody($parsed);
}
return $next($request, $response);
}
示例15: enrichRequestWithParsedBody
public function enrichRequestWithParsedBody(ServerRequestInterface $request)
{
if ($request->hasHeader(HeaderName::CONTENT_TYPE) && $request->getHeaderLine(HeaderName::CONTENT_TYPE) === 'application/json') {
$parsedBody = $this->serializer->deserialize($request->getBody()->__toString(), 'array', 'json');
return $request->withParsedBody($parsedBody);
} else {
return $request->withParsedBody([]);
}
}