tags:

views:

21

answers:

2

Let's say I need to call a webservice several times in the same php function, to avoid my page hanging out if webservice fails for some reason, is it enough to set_time_limit in php ? What's the best practices ?

Also I just read set_time_limit doesn't work when in safe_mode then what should I use ?

+1  A: 

Depending on how you're calling the web service, you may specify a timeout.

  • With the http wrapper, there's the timeout wrapper context option for connection timeout.
  • If the connection is already established and you're manipulating, PHP streams, you can use stream_set_timeout() to specify a timeout.
  • With curl, there are the options CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT and their millisecond variants.
  • For SoapClient, you can pass on construction a stream context, though I don't know its options (I imagine they may be the same as the http wrapper – I'll check this in a moment and then I'll update the answer). By code inspection, this stream context is passed to a transport stream (tcp or ssl) and therefore cannot be used to set a timeout. The timeout of a SoapClient can be set by changing the property _connection_timeout.
Artefacto
A: 

It depends on how you call the service. Sockets and streams have parameters for setting timeout on connecting or general timeout for the socket, which helps a lot.

You can give it a look on http://php.net/manual/en/function.stream-set-timeout.php and also check the manual for sockets if you use sockets.

bisko