PHP SoapFault 没有被异常处理程序捕获

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6374345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:10:17  来源:igfitidea点击:

PHP SoapFault not caught by exception handlers

phpsoapexception-handling

提问by mrtsherman

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.

我是 PHP 异常处理和 SOAP 的新手。出于某种原因,我无法捕获 SoapFault。我不知道为什么。肥皂服务器不是我的。

try { 
    $contact_id = $objSoapClient->getContactIdFromVisitorId('12345');
} 
catch (SoapFault $sf) { 
    echo "Soapfault"; 
} 
catch (Exception $e) { 
    echo "Exception"; 
}

I am purposely passing in the bad id 12345. When I enable errors I see the following message SoapFault exception: [SOAP-ENV:Client] Invalid Visitor ID. However, my catch SoapFault block nor my catch Exception block ever get hit. Why?

我故意传入错误 ID 12345。当我启用错误时,我看到以下消息SoapFault exception: [SOAP-ENV:Client] Invalid Visitor ID。但是,我的 catch SoapFault 块和我的 catch Exception 块都被击中。为什么?

回答by Francois Deschenes

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 catchclauses or instanceofexpressions are permitted because the missing class could be loaded later.

话虽如此,如果代码位于定义命名空间的类中,您的代码将无法工作,因为它会尝试将 Exception 引用为不存在的 \namespace\Exception。允许“被动”引用,例如catch子句或instanceof表达式中的引用,因为可以稍后加载缺少的类。

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;

为了让它工作,你必须在类名前加上一个斜杠(即\Exception)来告诉PHP使用全局空间中的PHP(或者root,如果你想调用它)(PHP)而不是你的命名空间;

<?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.

你可以在这里找到很多关于命名空间的信息:http: //php.net/manual/en/language.namespaces.php

回答by mrtsherman

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.

问题原来是我的 SoapClient 声明。为了触发异常,必须设置一个异常参数。

$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 
));