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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pandas 的list追加问题
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题