【问题标题】:PHP SoapClient timeout error handlerPHP SoapClient 超时错误处理程序
【发布时间】:2012-12-14 07:39:41
【问题描述】:

我正在使用SoapClient 调用一些网络服务。我正在寻找一种机制,可以帮助我在网络服务离线或关闭时向用户显示一些错误。

因为我必须等待一段时间(15 秒)才能向用户显示任何错误。我像这样在SoapClient 中添加connection_timeout,用于超时。

$this->client = new SoapClient($clienturl,array('trace' => 1,
'exceptions'=> 1,
'connection_timeout'=> 15));   //$clienturl is webservice url

在页面的顶部,我也添加了这一行,

ini_set("default_socket_timeout", 15); // 15 seconds

在特定的超时间隔之后,我会像这样得到不同的SOAP-ERROR

SOAP-ERROR: Parsing WSDL: Couldn't load from $clienturl

所以我正在寻找一个错误处理程序来处理这些SOAP-ERROR,以便以人类可读的格式向用户显示这些错误处理程序,例如“服务器已关闭,过一段时间再试一次”。或者有没有办法处理超时错误?

【问题讨论】:

  • $clienturl 是外部网址吗?是https吗?
  • @noslone 是的。 $clienturl 是外部的,不是 https。
  • 在浏览器中输入url会发生什么?
  • @noslone 它的 web 服务 url,所以它给了我所有相关的信息,比如功能信息。等,当它起来。但是当它下降时,它什么也没有显示。

标签: php soap-client


【解决方案1】:

你可以把它放在try/catch中

try {
    $time_start = microtime(true);
    $this->client = new SoapClient($clienturl,array('trace' => 1,
        'exceptions'=> 1,
        'connection_timeout'=> 15
    ));
} catch (Exception $e) {
    $time_request = (microtime(true)-$time_start);
    if(ini_get('default_socket_timeout') < $time_request) {
        //Timeout error!
    } else {
        //other error
        //$error = $e->getMessage();
    }
} 

【讨论】:

  • 正确和预期的答案。谢谢。
  • 这里 ini_get('default_socket_timeout') 在 senonds 中,对吗?和(microtime(true)-$time_start)in micro seconds 那么你能像ini_get('default_socket_timeout') &lt; $time_request那样编译谁@
【解决方案2】:

这就是我在 php 中用于soapClien 连接的方法

set_error_handler('error_handler');

function connectSoapClient($soap_client){
    while(true){
        if($soap_client['soap_url'] == ''){
            trigger_error("Soap url not found",E_USER_ERROR);
            sleep(60);
            continue;
        }
        try{
            $client = @new SoapClient($soap_client['soap_url'],array("trace" => 1,"exceptions" => true));
        }
        catch(Exception $e){
            trigger_error("Error occured while connection soap client<br />".$e->getMessage(),E_USER_ERROR);
            sleep(60);
            continue;
        }
        if($client){
            break;  
        }
    }
    return $client;
}


function error_handler($errno, $errstr, $errfile, $errline){
    if($errno == E_USER_ERROR){
        $error_time = date("d-m-Y H:i:s");
        $errstr .= "\n
            ############################### Error #########################################\n
            Error No: $errno 
            Error File: $errfile  
            Line No: $errline 
            Error Time : $error_time \n
            ##############################################################################
        ";
        mail($notify_to,$subject,$errstr);
    }
}

【讨论】:

  • 感谢您的回复,我正在寻找超时错误处理程序。所以我想知道我的SoapClient 调用是否成功,然后我的 web 服务出现故障,并且我正在调用相同 web 服务的函数,那时我再次得到相同的 SOAP-ERROR。那么有没有通用的解决方案来处理这个问题呢?
  • 在这种情况下,soap 客户端的任何错误都会被try catch 块捕获,所以如果它有任何错误,它将触发错误
猜你喜欢
  • 2012-02-25
  • 2015-05-07
  • 2011-03-30
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
相关资源
最近更新 更多