如何在PHP中使用NuSOAP处理超时?

时间:2012-12-11 16:33:41

标签: php web-services timeout nusoap

我正在使用NuSOAP为支付网关使用Web服务,但是,此网关的文档要求:

  • 如果doPayment()方法花费的时间超过300秒,则应立即执行getStatus()方法至少3次,以便尝试获得成功的响应。

问题是,我不知道如何使用PHP和NuSOAP处理该超时。当超时时,NuSOAP是否会返回任何特定的响应?我现在怎么实际超时?

以下是NuSOAP调用的一段代码:

$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

任何输入都将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:5)

我自己回答这个问题以供将来参考。首先,您需要扩展nusoap_client类的超时值。然后在您要测试的方法之前启动一个计时器,并将其与结束时间进行比较:

// 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