PHP SoapClient超时错误处理程序

时间:2012-12-14 07:39:41

标签: php soap-client

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

因为在向用户显示任何错误之前我必须等待一段时间(15秒)。我在这样的connection_timeout中添加了SoapClient,暂停了。

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

2 个答案:

答案 0 :(得分:6)

你可以把它放在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();
    }
} 

答案 1 :(得分: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);
    }
}