diff --git a/CHANGELOG.md b/CHANGELOG.md index cc9c98d72..9af8de7eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v3.2.6 + +### fixed + +- fix: json 中有 `&` 时解析错误(#687) + ## v3.2.5 ### fixed diff --git a/src/Parser/ArrayParser.php b/src/Parser/ArrayParser.php index d037c88af..5504848fd 100644 --- a/src/Parser/ArrayParser.php +++ b/src/Parser/ArrayParser.php @@ -23,17 +23,16 @@ public function parse(?ResponseInterface $response): array $body = (string) $response->getBody(); - if (Str::contains($body, '&')) { - return $this->query($body); - } - $result = json_decode($body, true); + if (JSON_ERROR_NONE === json_last_error()) { + return $result; + } - if (JSON_ERROR_NONE !== json_last_error()) { - throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]); + if (Str::contains($body, '&')) { + return $this->query($body); } - return $result; + throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]); } protected function query(string $body): array diff --git a/tests/Parser/ArrayParserTest.php b/tests/Parser/ArrayParserTest.php index b2fbd9fec..87f9e4513 100644 --- a/tests/Parser/ArrayParserTest.php +++ b/tests/Parser/ArrayParserTest.php @@ -61,4 +61,16 @@ public function testQueryBody() self::assertEqualsCanonicalizing(['name' => 'yansongda', 'age' => '29'], $result); } + + public function testJsonWith() + { + $url = 'https://yansongda.cn?name=yansongda&age=29'; + + $response = new Response(200, [], json_encode(['h5_url' => $url])); + + $parser = new ArrayParser(); + $result = $parser->parse($response); + + self::assertEquals('https://yansongda.cn?name=yansongda&age=29', $result['h5_url']); + } }