4

we are working with webservices but sometimes, they don't respond, they are too long to respond.

How to stop cURL if it takes more than 1 second ?

I tried :

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);

I Also tried to create a "temporary page" between my server and the webservice : my server calls a temporary page with :

set_time_limit(1);
ini_set('max_execution_time', 1);

And this temporary page calls the webservice itself with curl, but still nothing. If my webservice has a time execution of 10 seconds, i'll have to wait 10 seconds.

Any ideas ?

d3cima
  • 729
  • 1
  • 10
  • 31
  • 1
    The timeout approach should be the one to follow since the maximum execution time you are using in your proxy script is referring only to the actual script execution, the time spent on external calls is not counted. You can see this in a note on the manual page for the function you are using http://php.net/set_time_limit – mishu Mar 23 '15 at 09:25
  • thanks for this answer. The first solution I tried was not successful either. – d3cima Mar 23 '15 at 09:28
  • 1
    If you are running PHP on unix server try adding curl_setopt($ch, CURLOPT_NOSIGNAL, 1); – dafyk Mar 23 '15 at 09:32

1 Answers1

7

There is a best solution.

Setting Curl's Timeout in PHP

See documentation: http://www.php.net/manual/en/function.curl-setopt.php

CURLOPT_CONNECTTIMEOUT - The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds

Also don't forget to enlarge time execution of php script self:

set_time_limit(0); // to infinity for example


Community
  • 1
  • 1
Liang Rongze
  • 124
  • 3