How to handle timeouts with NuSOAP in PHP?

Dan H picture Dan H · Dec 11, 2012 · Viewed 7.9k times · Source

I am using NuSOAP to consume a webservice for a payment gateway, however, the documentation for this gateway requires that:

  • If the doPayment() method takes more than 300 seconds, it should immediately execute the getStatus() method at least 3 times in order to try to get a successful response.

The thing is, I don't know how to handle that timeout with PHP and NuSOAP. Does NuSOAP returns any specific response when there's a timeout? how do I now it actually timed out?

Here is a piece of code for the NuSOAP call:

$client = new nusoap_client( 'http://webserviceurl?wsdl...' , 'wsdl');

$err = $client->getError();
if ($err)
    die('Constructor error: ' . $err);

$proxy = $client->getProxy();

$payTrans = $proxy->doPayment(array('someparams' => 'somevalues'));

// if doPayment() timed out, then run the getStatus() method

Any input would be greatly appreciated. Thank you!

Answer

Dan H picture Dan H · Dec 19, 2012

I'm answering it myself for future reference. First of all you need to extend the timeout value for the nusoap_client class. Then just start a timer before the method you want to test and compare it to the time it took to end:

// Extending the timeout value to 300 seconds
$client = new nusoap_client( 'http://webserviceurl?wsdl...' , 'wsdl' , false, false, false, false, 0, 300)

$err = $client->getError();
if ($err)
    die('Constructor error: ' . $err);

$proxy = $client->getProxy();

$start = time(); // starting the timer
$payTrans = $proxy->doPayment(array('someparams' => 'somevalues'));
$timing = time() - $start; // calculating the transaction time

if($timing > 90 && $timing < 310)
    // It timed out: send an email, run another method, etc