代码之家  ›  专栏  ›  技术社区  ›  Jeremy French

检查URL是否有效(从PHP SOAP客户端)

  •  13
  • Jeremy French  · 技术社区  · 16 年前

    我正在编写一个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”选项和(在绝望中)use soap错误处理程序函数。

    5 回复  |  直到 14 年前
        1
  •  22
  •   Henrik Opel    16 年前

    你在使用xdebug吗?根据 this PHP bug report and discussion 至少从php 5.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错误,或者与另一个第三方组件类似的问题。

        2
  •  1
  •   rogeriopvl    16 年前

    引用 SoapClient documentation :

    exceptions选项是一个布尔值,用于定义SOAP错误是否引发soapfault类型的异常。

    所以你应该尝试以下方法:

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

    这条路会扔 SoapFault 允许您捕获它们的异常。

        3
  •  1
  •   kenorb    14 年前

    见: 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();
         }
       }
    
        4
  •  0
  •   Gabriel Solomon    16 年前

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

        5
  •  0
  •   user261873    16 年前

    关于您的信息,我正在使用soapclient和phpunit测试远程Web服务,但遇到了同样的问题!

    • 当使用旧的phpunit版本(3.3.x)作为第三方时,phpunit崩溃
    • 当使用当前版本的phpunit(3.4.6)作为第三方时,phpunit显示“runtimeexception”。

    这是我的第一个测试方法:

    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
    

    NB:我发现了一张关于这个主题的phpunit罚单: ticket 417

    推荐文章