¿Cómo manejo múltiples fallas de jabón durante la solicitud de cliente SOAP PHP?
-
12-12-2019 - |
Pregunta
Estoy interesado en hacer una llamada de SOAP a través de Soapclient de PHP a un servicio web para obtener el nivel de agua de una estación de monitoreo. Quiero manejar dos faults de jabón que han ocurrido durante la ejecución. La primera falla es la siguiente:
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}
Se espera que este error ocurra varias veces durante el script si los datos durante un cierto tiempo no están disponibles. Necesito captar esta falla para decirle al script que vuelva a intentarlo con una nueva hora. Usé un bloque de captura para hacerlo.
También debo capturar una segunda falla que ocurre si el servicio web no está cargando el archivo WSDL o el servidor es un momento. Para probar esto, ha dado mi script una ubicación errónea para generar el mismo error que había recibido anteriormente y es la siguiente:
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
El segundo error sigue siendo desconocido y termina mi script. Sin embargo, necesito atraparlo y mostrar un mensaje.
He publicado mi función PHP que hace que la llamada de SOAP sea inferior.
¿Podría alguien darme alguna idea sobre cómo hacer esto?
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
Solución
Excepción con respecto a WSDL roto solo puede ocurrir cuando llame Soapclient :: Constructor para
try {
$client= new SoapClient($wsdlUrl ,array('trace'=>false));
}catch(Exception $e) {
// your loging regarding this case
}
La excepción de SoapFault puede ocurrir cuando realiza un servicio web todo lo que:
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;