6

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

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

$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问题,以便以人类可读的格式向用户显示这些错误处理程序,例如“服务器已关闭,过一段时间再试一次”。或者有没有办法处理超时错误?

4

2 回答 2

7

你可以把它放在 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();
    }
} 
于 2012-12-14T08:42:37.170 回答
1

这就是我在 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);
    }
}
于 2012-12-14T08:53:17.870 回答