PHP SOAP错误捕获

Dav*_*vid 23 php soap

我正在变得绝望,我想要的只是简单的错误处理,当PHP SOAP Web服务关闭以回显错误消息登录服务时.请帮我!

目前它仍然显示错误(以及警告......):

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";
}
?>
Run Code Online (Sandbox Code Playgroud)

Mea*_*cle 30

2018年7月更新

如果您不关心获取SoapFault详细信息并且只想捕获来自SoapClient的任何错误,您可以在PHP 7+中捕获"Throwable".最初的问题是SoapClient在抛出SoapFault之前可能会出现"致命错误",所以通过使用Throwable捕获错误和异常,您将获得非常简单的错误处理,例如

try{
    soap connection...
}catch(Throwable $e){
    echo 'sorry... our service is down';
}
Run Code Online (Sandbox Code Playgroud)

如果您需要专门捕获SoapFault,请尝试原始答案,该答案应该允许您抑制阻止SoapFault被抛出的致命错误

原始答案

虽然打开上面评论中提到的异常是处理解析错误的一个很好的步骤,但它本身是不够的,因为SOAP可以使用本机php函数致命错误.

首先,您需要打开异常处理:

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';
}
Run Code Online (Sandbox Code Playgroud)

然后,您还需要使用自定义错误处理程序静默抑制源自SOAP的任何"PHP错误":

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
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您将找到它,而不是使用正确的消息"Soap错误:SOAP-ERROR:解析WSDL:无法从'...'加载"来跳过SoapFault异常,因此您最终会返回到能够处理的catch语句中错误更有效.


yok*_*oko 19

Fatal error: SOAP-ERROR: Parsing WSDL意味着WSDL是错误的,可能会丢失?所以它与肥皂无关.并且您无法通过try catch处理FATAL ERROR.请参阅此链接:http://ru2.php.net/set_error_handler#35622

当您尝试在浏览器中访问http://192.168.0.142:8080/services/Logon?wsdl时,您会得到什么?

您可以检查WSDL是否存在

$handle = curl_init($url);
curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
    /* You don't have a WSDL Service is down. exit the function */
}

curl_close($handle);

/* Do your stuff with SOAP here. */
Run Code Online (Sandbox Code Playgroud)

  • 嘿,让它工作,它不是404,如果无法访问服务则给出0,如果可以,则给出200.非常感谢你的帮助:) (2认同)

Bra*_*ent 8

不幸的是,当服务关闭/无法访问而不是返回SoapFault对象时,SOAP会抛出致命错误.

话虽这么说,你可以将它设置为抛出异常.您可能省略了将例外soap_client选项设置为false的部分

$client = new SoapClient("http://192.168.0.142:8080/services/Logon?wsdl",array(
    'exceptions' => false,    // change to true so it will throw an exception
));
Run Code Online (Sandbox Code Playgroud)

服务中断时捕获异常:

try {
  $client = new SoapClient("http://192.168.0.142:8080/services/Logon?wsdl",array(
    'exceptions' => true,
  ));
}
catch ( Exception $e )
{
    echo 'sorry... our service is down';
}
Run Code Online (Sandbox Code Playgroud)