PHP SoapFault not caught by exception handlers

后端 未结 2 1539
太阳男子
太阳男子 2020-12-31 02:29

I am new to PHP exception handling and SOAP. For some reason I cannot catch a SoapFault. I don\'t know why. The soap server is not mine.

try { 
    $contact_         


        
相关标签:
2条回答
  • 2020-12-31 03:16

    The code you've submitted appears to be correct. Here's the only thing that comes to my mind.

    With that said, if the code is located inside a class that define a namespace, you code will not work as it will try to reference Exception as \namespace\Exception which does not exist. "Passive" references such as those in catch clauses or instanceof expressions are permitted because the missing class could be loaded later.

    For it to work, you have to prefix the class name with a slash (i.e. \Exception) to tell PHP to use PHP from the global space (or root if you want to call it that) (PHP) as opposed to your namespace;

    <?php
    
    namespace test;
    
    class Foo
    {
      public function bar()
      {
        try
        {
          something_that_might_break();
        }
        catch (\Exception $e)
        {
          // this will work
        }
      }
    }
    
    ?>
    

    You can find lots of information about namespaces here: http://php.net/manual/en/language.namespaces.php.

    0 讨论(0)
  • 2020-12-31 03:29

    The problem turned out to be my SoapClient declaration. There is an exceptions parameter that must be set in order for the exceptions to trigger.

    $objSoapClient = new SoapClient('https://mywebservice.com/foo.wsdl', array(
        "trace" => false,  
        "exceptions" => true,     // <-------------- This!!!                                               
        'login' => 'username',    //username
        'password' => 'password', //password
        'features' => SOAP_SINGLE_ELEMENT_ARRAYS + SOAP_USE_XSI_ARRAY_TYPE 
    ));
    
    0 讨论(0)
提交回复
热议问题