2012-12-14 91 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'塊,所以如果它的任何錯誤,它會觸發錯誤 –