PHP SOAP错误捕获 [英] PHP SOAP error catching

查看:141
本文介绍了PHP SOAP错误捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到非常绝望,当PHP SOAP Web服务关闭以回显错误消息 login service down 时,我想要的只是简单的错误处理.请帮帮我!

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...):

Fatal error: SOAP-ERROR: Parsing 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
    }
}

然后您将发现它现在改为以正确的消息"Soap error:SOAP-ERROR:Parsing WSDL:无法从'...'加载"来触发SoapFault异常,因此您最终返回到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.

这篇关于PHP SOAP错误捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆