Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would curl ignore CURLOPT_TIMEOUT_MS (but honor CURLOPT_TIMEOUT)?

Tags:

php

curl

timeout

I am using curl to call a web service API. The service can unresponsive so I want to set a timeout. When I use CURLOPT_TIMEOUT things work as expected. But when I use CURLOPT_TIMEOUT_MS (note the 'MS' for milliseconds) the timeout doesn't appear to kick in at all. php.net tells me that the latter was available since PHP version 5.2.3, and I am using 5.2.6.

Any ideas why this is happening?

Thanks.

Code fragment:

$c = curl_init();
curl_setopt( $c, CURLOPT_URL, $call );
curl_setopt( $c, CURLOPT_HTTPHEADER, $headers); 
curl_setopt( $c, CURLOPT_HEADER, false );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $c, CURLOPT_TIMEOUT_MS, 100 ); 
curl_setopt( $c, CURLOPT_CONNECTIONTIMEOUT_MS, 100 ); 
$result = curl_exec($c);
curl_close($c);
like image 296
Greg Avatar asked Dec 06 '09 20:12

Greg


2 Answers

To close this question:

The version of curl I am using (7.15.5) doesn't support CURLOPT_TIMEOUT_MS. According to Greg I need at least 7.16.2.

like image 190
Greg Avatar answered Sep 18 '22 01:09

Greg


I have observed, similar issue on CentOS 5.4 system running curl version 4.3.0 and it seems whenever curl is built to use 'standard system name resolver', this behaviour kicks in.

Work around is to use 'CURLOPT_NOSIGNAL' option along with 'CURLOPT_TIMEOUT_MS'; however this may have it's own consequences.

Check out details on following page: https://ravidhavlesha.wordpress.com/2012/01/08/curl-timeout-problem-and-solution/

like image 40
Shrenik Avatar answered Sep 18 '22 01:09

Shrenik