公司中有不少服务是以curl或者soap方式连接第三方公司做的服务来交互数据,最近新增加了个需求,就是第三方服务发版时候,连接不上对方服务器时候要进行重试,其它原因导致的业务处理失败,则按失败处理,不会再进行调用。
思路就是判断curl或者soap连接不上对方服务器时候,抛出TimeoutException异常,捕获后做重试处理,其它错误导致的抛出的Exception则按失败处理。
curl处理
$ch = curl_init($url); $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 5, //5秒连接时间 CURLOPT_TIMEOUT => 30, //30秒请求等待时间 ); curl_setopt_array($ch, $options); $response = curl_exec($ch); if ($no = curl_errno($ch)) { $error = curl_error($ch); curl_close($ch); //$no错误码7为连接不上,28为连接上了但请求返回结果超时 if(in_array(intval($no), [7, 28], true)) { throw new TimeoutException('连接或请求超时' . $error, $no); } } curl_close($ch);
soap处理
php文档并没详细写soap超时或者连接不上返回的具体代码,业务处理失败或者连接不上等所有不成功,都会抛出一个SoapFault异常,看了下php的源码发现,还是有定义的
php源文件位置 /ext/soap/php_http.c
定义错误代码内容
add_soap_fault(this_ptr, "HTTP", "Unable to parse URL", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Unknown protocol. Only http and https are allowed.", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "SSL support is not available in this build", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Could not connect to host", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Failed Sending HTTP SOAP request", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Failed to create stream", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Error Fetching http headers", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Error Fetching http body, No Content-Length, connection closed or chunked data", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Redirection limit reached, aborting", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Didn't receive an xml document", NULL, err);
add_soap_fault(this_ptr, "HTTP", "Unknown Content-Encoding", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", "Can't uncompress compressed response", NULL, NULL);
add_soap_fault(this_ptr, "HTTP", http_msg, NULL, NULL);
从代码里可以看出来,连接不上都会返回一个HTTP码,soap并没像curl那样有具体的代码可以区分二者,只利用这个码可以判断是超时或者连接不上等网络问题
具体代码如下
ini_set('default_socket_timeout', 30); //定义响应超时为30秒 try { $options = array( 'cache_wsdl' => 0, 'connection_timeout' => 5, //定义连接超时为5秒 ); libxml_disable_entity_loader(false); $client = new \SoapClient($url, $options); return $client->__soapCall($function_name, $arguments); } catch (\SoapFault $e) { //超时、连接不上 if($e->faultcode == 'HTTP'){ throw new TimeoutException('连接或请求超时', $e->getCode()); } }
可以连接上soap服务,但客户端或者服务端出问题 $e->faultcode 会返回WSDL, 用这个来判断
以上为php使用soap和curl捕获请求超时和连接超时的方法。希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 《演灭Evotinction》正式登陆多平台发售!带你进入科幻潜行新境界
- 命运圣契2024公测最新兑换码大全 命运圣契开服可用兑换码汇总
- 银霞.1979-《回答·兰花草》新加坡限量复黑版[WAV+CUE]
- 王菲.1996-《乐乐精逊SACD日本限量版[WAV+CUE]
- 王菲.1996-《乐乐精逊SACD日本限量版[WAV+CUE]
- 不止黑神话!杭州给原创企业真金白银扶持 打造“杭州游戏”品牌
- 很有多样性:《战锤》首次引入非二元性别角色
- 《少女前线2:追放》项目组将裁员 官方:进一步优化组织结构
- 陈慧琳.2002-闪亮每一天新歌+精选2CD【正东】【WAV+CUE】
- 袁培华.1992-有缘【歌林】【WAV+CUE】
- 王瑞霞.1996-是我太软心【名冠】【WAV+CUE】
- 《谭维维 30首经典流行歌曲精选辑 2CD》[WAV/分轨][1.1GB]
- 《赵传 出道三十年全新大碟 你过得还好吗》[WAV/分轨][460MB]
- 《发烧四大天王 磁性男声HiFi发烧碟 2CD》[WAV/分轨][1GB]
- 黑神话悟空上品鼠司空精魄获取方法一览|上品鼠司空精魄收集攻略