如果您没有在 php.ini 文件中启用 php_soap.dll 扩展,尝试创建 的实例SoapClient
将导致 PHP 崩溃。
如果我像这样用 try-catch 块围绕 instatiation,
try{
$client = new SoapClient ($wsdl, array('cache_wsdl' => WSDL_CACHE_NONE) );
$result = $client->{$web_service}($parameters)->{$web_service."Result"};
return $result;
}
catch(Exception $e){
echo $e->getMessage();
}
它不会捕捉到异常。相反,它就像die()
在内部 PHP 代码中的某处被调用。有谁知道为什么会这样?
注意:我使用的是 PHP 7.2.1 版
正如 Arvind 已经指出的那样,错误和异常在 PHP 中是两种不同的东西。try/catch 仅适用于异常,不适用于错误。
这里有更多的解释你可以做什么:
错误处理或多或少是为应用程序全局定义的。内置错误处理检查错误的严重性,并根据此记录错误并停止执行。
您可以通过使用 set_error_handler() ( http://php.net/set_error_handler )设置自定义错误处理程序来覆盖此行为。
一种很常见的方法是定义一个引发异常的自定义错误处理程序。然后您就可以在代码中使用 try/catch 处理错误和异常。执行此操作的示例错误处理程序写在此处:http : //php.net/manual/en/class.errorexception.php。
从那里复制了最有趣的部分:
function exception_error_handler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
// This error code is not included in error_reporting
return;
}
throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");
如果将此代码放在靠近应用程序开头的某个位置,则不会引发错误,而是会引发 ErrorExceptions。这应该适用于 SoapClient 错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句