phpsoap-clientsoapfaultsoapexception

How do I handle multiple SOAP Faults during php Soap Client request?


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

Solution

  • 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;