PHP SOAP 错误捕获

PHP SOAP error catching(PHP SOAP 错误捕获)
本文介绍了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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)