2011-08-05 37 views
18

我越來越絕望,所有我想要的是簡單的錯誤處理,當PHP SOAP Web服務關閉以迴應錯誤消息登錄服務關閉。請幫幫我!PHP SOAP錯誤捕獲

在它仍然顯示錯誤的時刻(與警告一起...):

Fatal error: SOAP-ERROR: Parsing WSDL

下面是腳本:

<?php 
session_start(); 
$login="0000000000000nhfidsj"; //It is like this for testing, It will be changed to a GET 

$username = substr($login,0,13); //as password is always 13 char long 
           //(the validation is done int he javascript) 
$password = substr($login,13); 
try 
{ 
    ini_set('default_socket_timeout', 5); //So time out is 5 seconds 
    $client = new SoapClient("http://192.168.0.142:8080/services/Logon?wsdl"); //locally hosted 

    $array = $client->login(array('username'=>$username, 
            'password'=>$password)); 

    $result = $array->return; 

}catch(SoapFault $client){ 
    $result = "0"; 
} 

if($result == "true")//as this would be what the ws returns if login success 
{ 
    $_SESSION['user'] = $login; 
    echo "00"; 
} 
else 
{ 
    echo "01 error: login failed"; 
} 
?> 
+1

老實說,SOAP擴展中的任何致命錯誤都應該報告爲一個錯誤,因爲沒有任何情況會導致您的代碼導致致命錯誤。 404 WSDL應該像你期望的那樣是一個SoapFault。 – ColinM

回答

18

Fatal error: SOAP-ERROR: Parsing WSDL指的WSDL是錯誤的,也許失蹤?所以它與肥皂無關。你不能用try catch來處理致命錯誤。看到這個鏈接:http://ru2.php.net/set_error_handler#35622

當您在瀏覽器中嘗試訪問http://192.168.0.142:8080/services/Logon?wsdl時,您會得到什麼?

您可以檢查WSDL是現在的這個樣子

$handle = curl_init($url); 
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); 

$response = curl_exec($handle); 
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); 
if($httpCode == 404) { 
    /* You don't have a WSDL Service is down. exit the function */ 
} 

curl_close($handle); 

/* Do your stuff with SOAP here. */ 
+0

嗨,tnks的答覆,我得到一個基於用戶名和密碼我給它的響應,但我需要在錯誤顯示,如果服務器關閉,它是(因爲我斷開了我的網絡連接)。 。我得到那個致命的錯誤。 – David

+0

我編輯我的答案,你必須嘗試,如果該網址是有效的。和肥皂的東西 – yokoloko

+0

tnk u,看起來像什麼即時通訊尋找,它只是不工作xD和不幸的是我dnt reli有任何經驗與PHP cURL – David

2

也許是更好的選擇:

set_error_handler('my_error_handler'); 
set_exception_handler('my_exception_handler'); 

function my_exception_handler($e) { 
    exit('Error, something went terribly wrong: '.$e); 
} 

function my_error_handler($no,$str,$file,$line) { 
    $e = new ErrorException($str,$no,0,$file,$line); 
    my_exception_handler($e); 
} 

在那裏你可以調整上述功能的錯誤消息。 我使用它來返回與您所做的相同情況的消息,因爲它可能隨時發生。

假設您在首次登錄後發送肥皂消息,並且該響應永遠不會到達或僅部分到達,這樣您可以返回沒有任何腳本路徑,名稱和亞麻布的消息。 在這種情況下,我根本不會返回$ e,而只是輸出如下內容:'出錯了,請再試一次(稍後)。「

3

不幸的是,當服務關閉/無法訪問而不是返回SoapFault對象時,SOAP會引發致命錯誤。

這就是說,你可以將它設置爲拋出異常。你或許忽略你在哪裏soap_client選項設置例外假

$client = new SoapClient("http://192.168.0.142:8080/services/Logon?wsdl",array(
    'exceptions' => false, // change to true so it will throw an exception 
)); 

捕獲異常的一部分,當服務出現故障:

try { 
    $client = new SoapClient("http://192.168.0.142:8080/services/Logon?wsdl",array(
    'exceptions' => true, 
)); 
} 
catch (Exception $e) 
{ 
    echo 'sorry... our service is down'; 
} 
-6

的SOAPFault不延伸例外,趕上especific類型作品:

try { 
    $client = new SoapClient("http://192.168.0.142:8080/services/Logon?wsdl",array(
    'exceptions' => true, 
)); 
} 
catch (SoapFault $e) 
{ 
    echo 'sorry... our service is down'; 
} 
+3

它擴大例外! – Sven

17

雖然打開在上面的評論中提及的例外是處理解析錯誤一個很好的一步,這是不夠的它自己的SOAP可以提供fa使用本地php函數的tal錯誤。

所以首先你需要打開異常處理:

try { 
    $client = new SoapClient("http://192.168.0.142:8080/services/Logon?wsdl",array(
     'exceptions' => true, 
    )); 
} catch (SoapFault $e) { // Do NOT try and catch "Exception" here 
    echo 'sorry... our service is down'; 
} 

,然後你還需要使用自定義錯誤處理程序靜默抑制從SOAP發起任何「PHP錯誤」:

set_error_handler('handlePhpErrors'); 
function handlePhpErrors($errno, $errmsg, $filename, $linenum, $vars) { 
    if (stristr($errmsg, "SoapClient::SoapClient")) { 
     error_log($errmsg); // silently log error 
     return; // skip error handling 
    } 
} 

然後,您將立即找到它,而不是使用正確的消息「SOAP錯誤:SOAP-ERROR:解析WSDL:無法從'...'加載」來訪問SoapFault異常,因此您最終回到您的catch語句中,能夠更有效地處理錯誤。

+0

''exceptions'=> true'是默認的(在我的系統上,無論如何)。 –

+0

我試過set_error_handler,ob_start或register_shutdown_function,但實際上除了php_errors.log外,沒有任何東西可以抓住這個錯誤(windows php 7):-( –