PHP的guzzlehttp/guzzle库在碰到各种异常时的场景
PHP的guzzlehttp/guzzle库在碰到各种异常时的场景
结论:
经过测试得知
在http状态码为1xx, 2xx, 3xx时, 会在111处输出返回
在http状态码为4xx, 5xx时, 会在222处被捕获
在目标服务不可达或其他异常时会在333处被捕获
测试过程:
用其他程序写个接口, 实现输入什么状态码就返回什么状态码.
然后逐个http状态码测试
关掉服务, 再进行测试.
请求不存在的域名或IP时, 进行测试
测试环境:
guzzlehttp/guzzle: 7.9.2
php: 8.1
GuzzleHttp使用源码如下:
public function test()
{
$method = 'POST';
$url = 'http://127.0.0.1:9999/test?code=499';
$options = [];
try {
$http_response = (new \GuzzleHttp\Client())->request($method, $url, $options);
var_dump(111);//1xx
} catch (RequestException $e) {
$http_response = $e->getResponse();//5xx 4xx
var_dump(222);
} catch (\Exception $e) { //服务不可达
var_dump(333);
throw new ApiException($e->getMessage(), $e->getCode());
}
dd($http_response);
}