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?

4
  • is $clienturl an external url? And is it https? Commented Dec 14, 2012 at 7:54
  • @noslone Yes. $clienturl is external, and no its not https. Commented Dec 14, 2012 at 7:55
  • What happens when you enter the url in the browser? Commented Dec 14, 2012 at 7:57
  • @noslone Its webservice url, so it gives me all relevant information like function info. etc., when its up. But when its down its not showing anything. Commented Dec 14, 2012 at 8:00

2 Answers 2

6

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();
    }
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Correct and expected answer. Thanks.
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
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);
    }
}

2 Comments

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?
in this case any error with soap client will catch with try catch block so if its any error it will triggor error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.