IT评测·应用市场-qidao123.com技术社区

标题: PHP的guzzlehttp/guzzle库在遇到各种异常时的场景 [打印本页]

作者: 伤心客    时间: 2024-9-26 13:07
标题: 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使用源码如下:

  1. public function test()
  2.     {
  3.         $method = 'POST';
  4.         $url = 'http://127.0.0.1:9999/test?code=499';
  5.         $options = [];
  6.         try {
  7.             $http_response = (new \GuzzleHttp\Client())->request($method, $url, $options);
  8.             var_dump(111);//1xx
  9.         } catch (RequestException $e) {
  10.             $http_response = $e->getResponse();//5xx 4xx
  11.             var_dump(222);
  12.         } catch (\Exception $e) { //服务不可达
  13.             var_dump(333);
  14.             throw new ApiException($e->getMessage(), $e->getCode());
  15.         }
  16.         dd($http_response);
  17. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4