duanmu0834 2012-07-26 23:21
浏览 56
已采纳

如何在php Soap Client请求期间处理多个SOAP错误?

I am interested in making a soap call via php’s soapClient to a web service to get the water level from a monitoring station. I want to handle two soapfaults that have occured during the execution. The first fault is as follows :

SoapFault exception: [soapenv:Server.userException] java.rmi.RemoteException: We are sorry, but no data is available from this station at this time in C:\xampp\htdocs\NOAA\LogWriter.php:214 Stack trace: #0 C:\xampp\htdocs\NOAA\LogWriter.php(214): SoapClient->__soapCall('getWaterLevelRa...', Array, Array) #1 C:\xampp\htdocs\NOAA\LogWriter.php(188): getLevel('8531680', '20120726 15:19') #2 {main}

This error is expected to occur several times during the script if the data for a certain time is not available. I need to catch this fault in order to tell the script to try again with a new time. I used a catch block to do so.

I also need to catch a second fault that occurs if the webservice is not loading the wsdl file or the server is timedout. To test for this have gave my script a faultly location to generate the same error I had received previously and it is as follows:

Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://opendap.co-ops.nos.noaa.gov/axis/services/WaterLevelRawOneMin?wsdl' : Extra content at the end of the document in C:\xampp\htdocs\NOAA\LogWriter.php:210 Stack trace: #0 C:\xampp\htdocs\NOAA\LogWriter.php(210): SoapClient->SoapClient('http://opendap....', Array) #1 C:\xampp\htdocs\NOAA\LogWriter.php(171): getLevel('8531680', '20120726 12:35') #2 {main} thrown in C:\xampp\htdocs\NOAA\LogWriter.php on line 210  

The second error remains uncaught and terminates my script. However I need to catch it and display a message.

I have posted my php function that makes the soap call below.

Could anyone give me any ideas on how to do this?

function getLevel($id, $date) {

    $client = new SoapClient("http://opendap.co-ops.nos.noaa.gov/axis/services/WaterLevelRawOneMin?wsdl", array('trace' => false));

    $Parameters = array("stationId" => $id, "beginDate" => $date, "endDate" => $date, "datum" => "MLLW",
                        "unit"      => 1, "timeZone" => 1);

    try {
        return $client->__soapCall(
            "getWaterLevelRawOneMin", array('Parameters' => $Parameters),
            array('location' => "http://opendap.co-ops.nos.noaa.gov/axis/services/WaterLevelRawOneMin")
        );
    } catch (SoapFault $e) {
        if (
            $e->faultcode == "soapenv:Server.userException"
            and $e->faultstring == "java.rmi.RemoteException: We are sorry, but no data is available from this station at this time"
        ) {
            return "FAULT";
        } else {
            echo "Could not connect to the server";
        }
    } // end of catch blocK
}// end of function
  • 写回答

1条回答 默认 最新

  • douwen7331 2012-07-27 00:05
    关注

    Exception regarding broken WSDL can occur only when you call SoapClient::constructor so

    try {
        $client= new SoapClient($wsdlUrl ,array('trace'=>false));
    }catch(Exception $e) {
        // your loging regarding this case 
    }
    

    SoapFault exception can occur when you make a webservice all so:

    try {
        $client= new SoapClient($wsdlUrl ,array('trace'=>false));
        try {
           return $client->_call('....');
        } catch (SoapFault $sp) {
            //your logic rearding soap fault 
        }
    }catch(Exception $e) {
        // your loging regarding this case 
    }
    return false;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动