PHP SoapClient timeout error handler

I am calling some web services using SoapClient . I am looking for a mechanism to help me display some errors for the user when web services go offline or down.

How should I wait a while (15 seconds) before showing any errors to the user. I am adding connection_timeout to a SoapClient , like this, for a timeout.

 $this->client = new SoapClient($clienturl,array('trace' => 1, 'exceptions'=> 1, 'connection_timeout'=> 15)); //$clienturl is webservice url 

Also at the top of the page I added this line,

 ini_set("default_socket_timeout", 15); // 15 seconds 

After a certain period of time, I get different SOAP-ERROR like this,

 SOAP-ERROR: Parsing WSDL: Couldn't load from $clienturl 

So, I'm looking for an error handler that will handle these SOAP-ERROR to display those that are available in a human-readable format, for example, "Server is down, try again after a while." Or is there a way to handle timeout errors?

+4
source share
2 answers

You can put it in try / catch

 try { $time_start = microtime(true); $this->client = new SoapClient($clienturl,array('trace' => 1, 'exceptions'=> 1, 'connection_timeout'=> 15 )); } catch (Exception $e) { $time_request = (microtime(true)-$time_start); if(ini_get('default_socket_timeout') < $time_request) { //Timeout error! } else { //other error //$error = $e->getMessage(); } } 
+6
source

This is what I use to connect soapClien in php

 set_error_handler('error_handler'); function connectSoapClient($soap_client){ while(true){ if($soap_client['soap_url'] == ''){ trigger_error("Soap url not found",E_USER_ERROR); sleep(60); continue; } try{ $client = @new SoapClient($soap_client['soap_url'],array("trace" => 1,"exceptions" => true)); } catch(Exception $e){ trigger_error("Error occured while connection soap client<br />".$e->getMessage(),E_USER_ERROR); sleep(60); continue; } if($client){ break; } } return $client; } function error_handler($errno, $errstr, $errfile, $errline){ if($errno == E_USER_ERROR){ $error_time = date("dmY H:i:s"); $errstr .= "\n ############################### Error #########################################\n Error No: $errno Error File: $errfile Line No: $errline Error Time : $error_time \n ############################################################################## "; mail($notify_to,$subject,$errstr); } } 
+1
source

All Articles