fix: json 中有 & 时解析错误 (#687)

This commit is contained in:
yansongda 2022-09-26 20:20:11 +08:00 committed by GitHub
parent 0f6963a90f
commit 6a730b79c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

View File

@ -1,3 +1,9 @@
## v3.2.6
### fixed
- fix: json 中有 `&` 时解析错误(#687)
## v3.2.5
### fixed

View File

@ -23,17 +23,16 @@ class ArrayParser implements ParserInterface
$body = (string) $response->getBody();
$result = json_decode($body, true);
if (JSON_ERROR_NONE === json_last_error()) {
return $result;
}
if (Str::contains($body, '&')) {
return $this->query($body);
}
$result = json_decode($body, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]);
}
return $result;
throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]);
}
protected function query(string $body): array

View File

@ -61,4 +61,16 @@ class ArrayParserTest extends TestCase
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']);
}
}