php中curl和soap方式请求服务超时如何解决-创新互联
今天就跟大家聊聊有关php中curl和soap方式请求服务超时如何解决,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联专注于企业成都全网营销、网站重做改版、碑林网站定制设计、自适应品牌网站建设、H5响应式网站、商城建设、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为碑林等各大城市提供网站开发制作服务。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()); } }
看完上述内容,你们对php中curl和soap方式请求服务超时如何解决有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
新闻标题:php中curl和soap方式请求服务超时如何解决-创新互联
文章源于:http://pwwzsj.com/article/dodiso.html