开发者

PHP cURL timeout ignored

开发者 https://www.devze.com 2023-03-22 16:22 出处:网络
Using curl_setopt() I hav开发者_JAVA百科e set CURLOPT_CONNECTTIMEOUT_MS to 1000 (1 second) and have set up another script that sleeps for 5 seconds, then responds 200 OK (using sleep()) which I call f

Using curl_setopt() I hav开发者_JAVA百科e set CURLOPT_CONNECTTIMEOUT_MS to 1000 (1 second) and have set up another script that sleeps for 5 seconds, then responds 200 OK (using sleep()) which I call for testing purposes. My script always waits for the response, even though it should yield in a cURL timeout error.

How do I make the timeout work as expected and interrupt the request?

$ch = curl_init($url);
curl_setopt_array($ch, array(
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_FOLLOWLOCATION => TRUE,
  CURLOPT_NOBODY => TRUE,
  CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
  CURLOPT_CONNECTTIMEOUT_MS => 1000,
  CURLOPT_MAXREDIRS => 5,
  CURLOPT_USERAGENT => 'Linkit/2.x Drupal/7.x',
));
$document = curl_exec($ch);

I have also tried CURLOPT_TIMEOUT_MS and also the variants without the _MS suffixes.

I'm using PHP 5.3.4 with cURL 7.19.7 on OS X 10.6, XAMPP.


The CURLOPT_CONNECTTIMEOUT or CURLOPT_CONNECTTIMEOUT_MS define the maximum amount of time that cURL can take to connect to the server but in your case, the connection is successful so the time-out no longer applies.

You need to use CURLOPT_TIMEOUT or CURLOPT_TIMEOUT_MS which define the maximum amount of time cURL can execute for.

For a complete list of options supported by PHP, look at the curl_setopt documentation.


The curl library makes a system call and operates independently of php (sidenote: that's why it is possible to take advantage of multi-threading with curl, even though php itself doesn't support threading). So if you make the curl call and then sleep(), curl still runs.

Also, the timeout setting is for how long to wait for the request to timeout, not your script. For instance, if I make a curl request to google.com and google.com is taking forever to respond, the timeout setting lets me tell curl how long to sit around and wait for google.com to respond.

edit:

Okay, so you are saying you have your curl script that makes a request to another script, and that script has the sleep() in it. Okay, well the curl CURLOPT_CONNECTTIMEOUT (or _MS) setting is to tell curl how long to wait around for a response from the requested server - as in, a connection made. When the curl request is made, it is getting a response that a connection was made...then the sleep() is just delaying the output it's giving. So basically, "wait for a response" is not the same as "how long to timeout the curl execution"

What you want to use is CURLOPT_TIMEOUT or CURLOPT_TIMEOUT_MS


Well, I had the same problem and wasted so much time looking for the solution and found a working solution at the end.

I though I should share it here and this might be helpful for someone in future.

I have simply used both options. I have used 4 seconds and 8 seconds respectively.

curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($curl_session, CURLOPT_TIMEOUT, 8);
0

精彩评论

暂无评论...
验证码 换一张
取 消