检查URL是否有效(从php soap客户端)

检查URL是否有效(从php soap客户端),php,xdebug,soap-client,Php,Xdebug,Soap Client,我正在编写一个web应用程序,它允许用户为SoapClient指定URL。我想验证当用户提交表单时php是否可以连接到客户端。我认为我可以通过try-catch或set-error-handler(或两者的组合)来实现这一点。但是,对于致命错误,这似乎是不可能的。有没有办法让SoapClent测试一个不会抛出不可恢复错误的URL Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://example.com/wibble

我正在编写一个web应用程序,它允许用户为SoapClient指定URL。我想验证当用户提交表单时php是否可以连接到客户端。我认为我可以通过try-catch或set-error-handler(或两者的组合)来实现这一点。但是,对于致命错误,这似乎是不可能的。有没有办法让SoapClent测试一个不会抛出不可恢复错误的URL

Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://example.com/wibble'
我希望它标记一个错误,因为URL不存在,但我希望能够捕获它

否则,我想我可以自己尝试下载并验证URL,但我认为可以从SoapClient进行验证

这应该是一个致命的错误吗

编辑

在阅读了rogeriopvl的回答之后,我重新认识到我应该说我已经尝试了soapclient构造函数的“exceptions”选项和(在绝望中)使用soap错误处理函数。

引用:

exceptions选项是一个布尔值,定义soap错误是否引发SoapFault类型的异常

因此,您应该尝试以下方法:

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

这种方法将抛出异常,允许您捕获它们。

您可以尝试执行curl或fsockopen请求,以检查URL是否有效。

您是否使用xdebug?据介绍,这个问题至少在PHP5.1之后就已经解决了,但在某种程度上,它与“致命错误到异常的转换”相混淆,导致异常没有生成,致命错误“泄漏”

我可以在启用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错误,或者另一个第三方组件可能会出现类似问题。

关于您的信息,我正在使用带PHPUnit的SoapClient来测试远程Web服务,并且遇到了同样的问题

  • 当使用旧的PHPUnit版本(3.3.x)作为第三方时,PHPUnit崩溃
  • 当使用当前版本的PHPUnit(3.4.6)作为第三方时,PHPUnit显示“RuntimeException”
以下是我的第一个测试方法:

public function testUnavailableURL() { $client = new SoapClient("http://wrong.URI"); } public function testUnavailableURL() { try { $client = @new SoapClient("http://wrong.URI"); } catch (SoapFault $fault) { print "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})"; } } 公共函数testUnavailableURL(){ $client=新的SoapClient(“http://wrong.URI"); } 以下是PHPUnit的第一个结果:

There was 1 error: 1) MyTestCase::testUnavailableURL RuntimeException: FAILURES! 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 有1个错误: 1) MyTestCase::testUnavailableURL 运行时异常: 失败! 这是我的第二个测试方法:

public function testUnavailableURL() { $client = new SoapClient("http://wrong.URI"); } public function testUnavailableURL() { try { $client = @new SoapClient("http://wrong.URI"); } catch (SoapFault $fault) { print "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})"; } } 公共函数testUnavailableURL(){ 试一试{ $client=@new SoapClient(“http://wrong.URI"); }catch(SoapFault$fault){ 打印“SOAP错误:(错误代码:{$Fault->faultcode},错误字符串:{$Fault->faultstring}”; } } 以下是PHPUnit第二次测试结果:

There was 1 error: 1) MyTestCase::testUnavailableURL RuntimeException: FAILURES! 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 3.4.6。 .SOAP错误:(错误代码:WSDL,错误字符串:SOAP-ERROR:解析WSDL:无法从加载'http://wrong.URI“:未能加载外部实体”http://wrong.URI" )... 时间:3秒,内存:4.25Mb 好啊 注意:我发现了一张关于这个主题的phpunit罚单:

参见:

可能的解决办法:

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

我试过了,没用,应该把它放在问题里,我的错,我会更新。这将检查是否可以从URL获得一些东西,但是我不会检查它是否是一个真正的WSDL文件。它对我来说也是新的-没有什么比搜索由调试器本身引起的bug更烦人的了:/这是一个Xdebug bug,但我最近已经修复了它。但它还不是发布的一部分。@Derick是否有包含此修复程序的XDebug发布的计划日期?我也有同样的问题。我正在尝试运行xdebug_disable();如前所述,它引发了另一个致命错误,该函数不存在:\Xdebug github存储库中已经有一个更好的修复程序。