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.
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.
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.
Several factors could cause this, including a slow network connection, network congestion, or a low connection timeout limit on the server.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With