• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    php中curl和soap方式请求服务超时问题的解决

    公司中有不少服务是以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捕获请求超时和连接超时的方法。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    您可能感兴趣的文章:
    • 解决mysql服务器在无操作超时主动断开连接的情况
    • 小程序server请求微信服务器超时的解决方法
    • 浅谈java中异步多线程超时导致的服务异常
    • 详解Nginx服务器中配置超时时间的方法
    • Win7系统日志提示在没有配置的 DNS 服务器响应之后,名称“域名”的名称解析超时的解放方法
    • oracle远程连接服务器出现 ORA-12170 TNS:连接超时 解决办法
    • 使用FileZilla连接时超时无法连接到服务器
    • SNMP4J服务端连接超时问题解决方案
    上一篇:Laravel框架模板继承操作示例
    下一篇:yii2中关于加密解密的那些事儿
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    php中curl和soap方式请求服务超时问题的解决 php,中,curl,和,soap,方式,请求,