Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl timeout less than 1000ms always fails?

This code always fails (i.e., $result is Boolean false):

     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $path);
     curl_setopt($ch, CURLOPT_HEADER, TRUE);
     curl_setopt($ch, CURLOPT_NOBODY, TRUE);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     $curl_version = curl_version();

     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 999);

     $result = curl_exec($ch);
     curl_close($ch);

This code always succeeds (i.e., $result is a string containing the header):

     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $path);
     curl_setopt($ch, CURLOPT_HEADER, TRUE);
     curl_setopt($ch, CURLOPT_NOBODY, TRUE);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     $curl_version = curl_version();

     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1000);

     $result = curl_exec($ch);
     curl_close($ch);

The only difference is that I've changed the timeout from 999ms to 1000ms.

This must be either a bug in curl or some sort of minimum in the documentation for connection timeouts that I missed. Which is it? My money is on the latter.

like image 922
Trott Avatar asked Nov 02 '11 21:11

Trott


People also ask

How do I increase my cURL timeout limit?

To set a timeout for a Curl command, you can use the --connect-timeout parameter to set the maximum time in seconds that you allow Curl to connect to the server, or the --max-time (or -m) parameter for the total time in seconds that you authorize the whole operation.

How do you pass timeout in cURL command?

Tell curl with -m / --max-time the maximum time, in seconds, that you allow the command line to spend before curl exits with a timeout error code (28). When the set time has elapsed, curl will exit no matter what is going on at that moment—including if it is transferring data.

What causes a cURL timeout?

Several factors could cause this, including a slow network connection, network congestion, or a low connection timeout limit on the server.

Does cURL have a timeout?

cURL has an optional flag '--connect-timeout' where you can specify the duration in seconds. If you have a version of cURL that is 7.32. 0 or later, you can also specify the duration as decimal values. The value that you specify will set the maximum time duration to wait for a reply back from the remote server.


1 Answers

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

The number of milliseconds to wait while trying to connect. Use 0 to wait indefinitely. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second.

like image 141
dev-null-dweller Avatar answered Oct 11 '22 22:10

dev-null-dweller