6

I am calling some web services, using SoapClient. I am looking for a mechanism which will help me to display some errors to user, whenever web services goes offline or down.

As I have to wait for some time(15 sec) before displaying any errors to user. I am adding connection_timeout in SoapClient like this, for timeout.

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

Also in top section of page, I have added this line,

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

After specific timeout interval I am getting different SOAP-ERROR like this,

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

So I am looking for an error handler which will handle these SOAP-ERROR so as to display those in human-readable format to user like "Server is down, Try again after some time." Or Is there any way to handle timeout errors?

hakre
  • 193,403
  • 52
  • 435
  • 836
Vinay
  • 2,564
  • 4
  • 26
  • 35

2 Answers2

7

You can put it in a 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();
    }
} 
noslone
  • 1,301
  • 10
  • 14
  • Correct and expected answer. Thanks. – Vinay Dec 19 '12 at 06:03
  • here `ini_get('default_socket_timeout')` is in senonds right? and `(microtime(true)-$time_start)`in micro seconds then who can you compire like `ini_get('default_socket_timeout') < $time_request` – Unnikrishnan Mar 19 '21 at 09:43
1

This is what I am using for soapClien connection 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("d-m-Y 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);
    }
}
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
  • Thanks for reply, I am looking for timeout error handler. So I want to know if my `SoapClient` Call is successful, and after that my webservice goes down, and I am calling a function of same webservice, that time I am again getting same SOAP-ERROR. so is there any generic solution for handling this issue too? – Vinay Dec 14 '12 at 10:13
  • in this case any error with soap client will catch with `try` `catch` block so if its any error it will triggor error – Miqdad Ali Dec 14 '12 at 10:49