Domanda

Sono interessato a creare una chiamata di sapone tramite PHP's SoapClient a un servizio Web per ottenere il livello dell'acqua da una stazione di monitoraggio. Voglio gestire due soapfault che si sono verificati durante l'esecuzione. Il primo errore è il seguente:

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}
.

Questo errore si prevede che si verifichi più volte durante lo script se i dati per un determinato momento non sono disponibili. Ho bisogno di catturare questo errore per dire allo script di riprovare con un nuovo tempo. Ho usato un blocco di cattura per farlo.

Ho inoltre bisogno di catturare un secondo errore che si verifica se il WebService non sta caricando il file WSDL o il server è Timedout. Per verificare questo ha dato il mio script una posizione di errore per generare lo stesso errore che avevo ricevuto in precedenza ed è come segue:

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  
.

Il secondo errore rimane non rilevato e termina il mio script. Tuttavia ho bisogno di prenderlo e visualizzare un messaggio.

Ho pubblicato la mia funzione PHP che rende la chiamata di sapone qui sotto.

Qualcuno potrebbe darmi qualche idea su come fare questo?

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
.

È stato utile?

Soluzione

L'eccezione per quanto riguarda il WSDL rotto può verificarsi solo quando si chiama SOAPCLIENT :: Costruttore SO

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

Excepture Soapfault può verificarsi quando si effettua un WebService tutti:

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top