PHP SOAP 错误捕获
我快绝望了,我想要的只是在 PHP SOAP Web 服务关闭时进行简单的错误处理以回显错误消息登录服务关闭.请帮帮我!
I'm getting desperate, all I want is simple error handling when the PHP SOAP Web Service is down to echo an error message login service down. Please help me!
目前它仍然显示错误(以及警告......):
At the moment it's still displaying the error (along with warnings...):
致命错误:SOAP-ERROR:解析 WSDL
这是脚本:
<?php
session_start();
$login="0000000000000nhfidsj"; //It is like this for testing, It will be changed to a GET
$username = substr($login,0,13); //as password is always 13 char long
//(the validation is done int he javascript)
$password = substr($login,13);
try
{
ini_set('default_socket_timeout', 5); //So time out is 5 seconds
$client = new SoapClient("http://192.168.0.142:8080/services/Logon?wsdl"); //locally hosted
$array = $client->login(array('username'=>$username,
'password'=>$password));
$result = $array->return;
}catch(SoapFault $client){
$result = "0";
}
if($result == "true")//as this would be what the ws returns if login success
{
$_SESSION['user'] = $login;
echo "00";
}
else
{
echo "01 error: login failed";
}
?>
推荐答案
2018 年 7 月更新
如果您不关心获取 SoapFault 详细信息,而只想捕获来自 SoapClient 的任何错误,您可以在 PHP 7+ 中捕获Throwable".最初的问题是 SoapClient 在抛出 SoapFault 之前可以致命错误",因此通过使用 Throwable 捕获错误和异常,您将获得非常简单的错误处理,例如
If you don't care about getting the SoapFault details and just want to catch any errors coming from the SoapClient you can catch "Throwable" in PHP 7+. The original problem was that SoapClient can "Fatal Error" before it throws a SoapFault so by catching both errors and exceptions with Throwable you will have very simple error handling e.g.
try{
soap connection...
}catch(Throwable $e){
echo 'sorry... our service is down';
}
如果您需要专门捕获 SoapFault,请尝试原始答案,该答案应该允许您抑制防止抛出 SoapFault 的致命错误
If you need to catch the SoapFault specifically, try the original answer which should allow you to suppress the fatal error that prevents the SoapFault being thrown
与旧 PHP 版本相关的原始答案
SOAP 可能会在内部调用原生 php 函数时发生致命错误,从而防止引发 SoapFaults,因此我们需要记录并抑制这些原生错误.
SOAP can fatal error calling the native php functions internally which prevents the SoapFaults being thrown so we need to log and suppress those native errors.
首先需要开启异常处理:
First you need to turn on exceptions handling:
try {
$client = new SoapClient("http://192.168.0.142:8080/services/Logon?wsdl",array(
'exceptions' => true,
));
} catch ( SoapFault $e ) { // Do NOT try and catch "Exception" here
echo 'sorry... our service is down';
}
然后,您还需要使用自定义错误处理程序静默抑制源自 SOAP 的任何PHP 错误":
AND THEN you also need to silently suppress any "PHP errors" that originate from SOAP using a custom error handler:
set_error_handler('handlePhpErrors');
function handlePhpErrors($errno, $errmsg, $filename, $linenum, $vars) {
if (stristr($errmsg, "SoapClient::SoapClient")) {
error_log($errmsg); // silently log error
return; // skip error handling
}
}
然后您会发现它现在会触发 SoapFault 异常并显示正确的消息Soap 错误:SOAP-ERROR:解析 WSDL:无法从 '...' 加载",因此您最终返回到您的 catch 语句中能够更有效地处理错误.
You will then find it now instead trips a SoapFault exception with the correct message "Soap error: SOAP-ERROR: Parsing WSDL: Couldn't load from '...'" and so you end up back in your catch statement able to handle the error more effectively.
相关文章