Question

What happens when you use a negative curl timeout?

$curlOptions[CURLOPT_TIMEOUT] = -2;

Does it get transformed into positive 2, or remains -2?

Because, sometimes curl works, sometimes doesn't...

Was it helpful?

Solution

This is how curl works:

First it connects to the url that you provide.

Then it perform the overall curl operation for the amount of seconds you have specified through the CURLOPT_TIMEOUT option.

So if you set the CURLOPT_TIMEOUT as -2 it will never execute after connecting to the host. To verify this, run the script by enabling the CURLOPT_VERBOSE option. And you'll get the following msg always.

* Operation timed out after -2000 milliseconds with 0 bytes received

OTHER TIPS

Why do you need a negative timeout ? it doesn't make any sense. Either curl gets the page and quits or wait until the it's completes loading, for that you use a POSITIVE value, bellow it's an example on how to use curl with timeout:

$ch = curl_init('http://site/test/test_timeout.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds - You can use 0 for infinite timeout
/* You can also use
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000); //timeout in miliseconds (5 seconds)
*/
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);

Curl Timeout Options:

CURLOPT_TIMEOUT
The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
CURLOPT_TIMEOUT_MS
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. Added in cURL 7.16.2. Available since PHP 5.2.3.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top