2012-12-14 92 views
4

我打电话给一些Web服务,使用SoapClient。我正在寻找一种机制,这将帮助我向用户显示一些错误,无论何时Web服务脱机或关闭。PHP 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的错误处理程序,以便以用户可读的格式显示给用户,如“服务器已关闭,一段时间后再试一次”。或者有什么办法来处理超时错误?

+0

soapClien连接'$ clienturl'外部网址是什么?它是https吗? – noslone

+0

@noslone是的。 $ clienturl是外部的,没有它不是https。 – Vinay

+0

当您在浏览器中输入网址时会发生什么? – noslone

回答

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(); 
    } 
} 
+0

正确和预期的答案。谢谢。 – Vinay

1

这是我在用在PHP

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); 
    } 
} 
+0

感谢您的回复,我正在寻找超时错误处理程序。所以我想知道我的'SoapClient'调用是否成功,之后我的web服务关闭了,并且我调用了同一个web服务的一个函数,那时候我又一次得到了相同的SOAP-ERROR。那么是否还有处理这个问题的通用解决方案呢? – Vinay

+0

在这种情况下,与肥皂客户端的任何错误将赶上'try'' catch'块,所以如果它的任何错误,它会触发错误 –