diff --git a/composer.json b/composer.json index 463ae09..699871d 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "tansilu/hfpay-lib", - "version": "0.0.7", + "version": "0.0.8", "type": "library", "require": { "php": ">=7.4", diff --git a/src/http/Response.php b/src/http/Response.php index 7b68450..fcbad98 100644 --- a/src/http/Response.php +++ b/src/http/Response.php @@ -2,6 +2,7 @@ namespace Tansilu\HfPayLib\http; +use GuzzleHttp\Exception\InvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Tansilu\HfPayLib\exception\ApiException; @@ -11,7 +12,7 @@ class Response * @var int 状态码 */ private int $statusCode = 200; - private array $body = []; + private ?array $body = []; private string $source; @@ -34,7 +35,14 @@ class Response switch ($bodyFormat[0]) { case 'text/html': case 'application/json': - $self->body = json_decode($self->source, true); + try { + $deRet = json_decode($self->source, true); + if($deRet != null) { + $self->body = $deRet; + } + } catch (InvalidArgumentException $e) { + throw new ApiException('反馈的消息格式错误'); + } break; case 'application/xml': case 'text/plain': @@ -45,7 +53,7 @@ class Response return $self; } - public static function tempInit(int $code, array $body) + public static function tempInit(int $code, ?array $body): Response { $self = new self(); $self->statusCode = $code;