From af88f79f022a5dd0b34206a612f17ba62db11c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 10 Sep 2021 14:06:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-message/Parse.php | 10 +++++----- http-message/ServerRequest.php | 5 ++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/http-message/Parse.php b/http-message/Parse.php index 7353f287..dc52cd12 100644 --- a/http-message/Parse.php +++ b/http-message/Parse.php @@ -12,14 +12,14 @@ class Parse * @param $content * @param $contentType * @return mixed + * @throws \Exception */ public static function data($content, $contentType): mixed { - var_dump($contentType, $content); - if (str_contains($contentType, 'json')) { - return json_encode($content,true); + if (empty($content)) { + return null; } - if (str_contains($contentType, 'xml')) { + if (str_starts_with($content, '<') || str_contains($contentType, 'xml')) { return Xml::toArray($content); } if (str_contains($contentType, 'x-www-form-urlencoded')) { @@ -29,7 +29,7 @@ class Parse if (str_contains($contentType, 'serialize')) { return unserialize($content); } - return $content; + return json_encode($content, true); } } diff --git a/http-message/ServerRequest.php b/http-message/ServerRequest.php index 7fd37c5d..a293989e 100644 --- a/http-message/ServerRequest.php +++ b/http-message/ServerRequest.php @@ -78,10 +78,9 @@ class ServerRequest extends Request implements ServerRequestInterface ->withMethod($request->getMethod()) ->withParsedBody(function (StreamInterface $stream, ?array $posts) { try { - $content = $stream->getContents(); - var_dump($content, $posts); + $content = Parse::data($stream->getContents(), $this->getContentType()); if (!empty($content)) { - return Parse::data($content, $this->getContentType()); + return $content; } return $posts; } catch (\Throwable $throwable) {