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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值