2009-09-10 22 views
13

我在寫一個web應用程序,它將允許用戶指定一個SoapClient的URL。我想驗證當用戶提交表單時,php可以連接到客戶端。我想你可以通過try catch或者set_error_handler(或者兩者的組合)來做到這一點。但是看起來這是致命錯誤不可能的。有沒有辦法讓SoapClent測試一個不會引發不可恢復的錯誤的URL?檢查一個URL是有效的(來自php肥皂客戶端)

Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://example.com/wibble' 

我想標記一個錯誤,因爲URL不存在,但我希望能夠抓住它。

否則,我想我可以嘗試自己下載並驗證URL,但我一直認爲它可以從SoapClient中完成。

這是致命的錯誤嗎?

編輯

閱讀rogeriopvl的回答後,我reaslise,我應該說,我曾經嘗試過「例外」選項給SoapClient的構造函數和(在絕望中)使用皁錯誤處理函數。

回答

22

您使用的是xdebug嗎?根據this PHP bug report and discussion,這個問題至少在PHP5.1之後得到了修正,但this xdebug bug以'異常轉換致命錯誤'的方式混淆,這種異常不會產生,並且致命錯誤'泄漏通過'。

我可以在本地複製本,並啓用了XDebug:

try { 
    $soapClient = new SoapClient('http://www.example.com'); 
} 
catch(Exception $e) { 
    $exceptionMessage = t($e->getMessage()); 
    print_r($exceptionMessage); 
} 

這給了我你所描述的致命錯誤,甚至沒有進入catch子句:

Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://www.example.com' 

它的工作原理,如果我禁用xdebug就在通話之前:

xdebug_disable(); 
try { 
    $soapClient = new SoapClient('http://www.example.com'); 
} 
catch(Exception $e) { 
    $exceptionMessage = t($e->getMessage()); 
    print_r($exceptionMessage); 
} 

這會觸發預期的異常,而我得到的catch子句在適當的SOAPFault對象與消息:

SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://www.example.com' 

所以基本上異常工作作爲標榜。如果它們不適用於您的案例,那麼您可能會遇到xdebug錯誤,或者可能與另一個第三方組件存在類似的問題。

+0

xdebug有bug。這對我來說是新的,謝謝。 – 2009-09-14 15:47:51

+4

這對我來說也是新的 - 沒有太多的東西比狩獵調試器本身引起的bug更煩人:/ – 2009-09-14 16:27:16

+1

這是一個Xdebug錯誤,但我最近修復了這個錯誤。儘管如此,它並不是發佈的一部分。 – Derick 2012-01-17 16:20:23

1

報價SoapClient documentation

例外選項定義肥皂錯誤是否拋出類型的SOAPFault的異常的布爾值。

所以,你應該嘗試類似:

$client = new SoapClient("some.wsdl", array('exceptions' => TRUE)); 

這樣會拋出異常SoapFault,讓您趕上他們。

+0

我試過了,沒有工作,應該把它的問題,我的壞,我會更新。 – 2009-09-10 20:50:20

0

你可以嘗試做一個curl或fsockopen請求來檢查URL是否有效。

+0

這將檢查可以從URL獲取的內容,但不會檢查它是否是真正的WSDL文件。 – 2009-09-14 10:02:21

0

爲了您的信息,我使用SoapClient和PHPUnit來測試遠程Web服務,並得到了同樣的問題!

    使用PHPUnit的當前版本(3.4.6)作爲第三方,PHPUnit的顯示器 「的RuntimeException」 時,使用的是舊版本的PHPUnit(3.3.x)作爲第三方,PHPUnit的崩潰

這是我的第一測試方法:

 
public function testUnavailableURL() { 
    $client = new SoapClient("http://wrong.URI"); 
} 

在這裏被PHPUnit的第一結果:

 
There was 1 error: 

1) MyTestCase::testUnavailableURL 
RuntimeException: 


FAILURES! 

這是我的第二測試方法:

 
public function testUnavailableURL() { 
     try { 
      $client = @new SoapClient("http://wrong.URI"); 
     } catch (SoapFault $fault) { 
      print "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})"; 
     } 
} 

這裏是PHPUnit的第二次測試結果:

 
PHPUnit 3.4.6 by Sebastian Bergmann. 

.SOAP Fault: (faultcode: WSDL, faultstring: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://wrong.URI' : failed to load external entity "http://wrong.URI" 
)... 

Time: 3 seconds, Memory: 4.25Mb 

OK 

注:我發現有關此話題的PHPUnit的票:ticket 417

1

參見:http://bugs.xdebug.org/view.php?id=249

可能的解決辦法:

Index: trunk/www/sites/all/libraries/classes/defaqtoSoapClient.class.php 
=================================================================== 
--- classes/defaqtoSoapClient.class.php 
+++ classes/defaqtoSoapClient.class.php 
@@ -31,10 +31,23 @@ 

    try { 
+  // xdebug and soap exception handling interfere with each other here 
+  // so disable xdebug if it is on - just for this call 
+  if (function_exists('xdebug_disable')) { 
+   xdebug_disable(); 
+  } 
     //Create the SoapClient instance 
     parent::__construct($wsdl, $options); 
    } 
    catch(Exception $parent_class_construct_exception) { 
+  if (function_exists('xdebug_enable')) { 
+   xdebug_enable(); 
+  } 
     // Throw an exception an say that the SOAP client initialisation is failed 
     throw $parent_class_construct_exception; 
+ } 
+ if (function_exists('xdebug_enable')) { 
+  xdebug_enable(); 
    } 
    } 
+0

更好的修復程序已經是Xdebug github存儲庫的一部分。 – Derick 2012-01-17 16:21:04