views:

476

answers:

1

We've written a script that pulls data from an external server. If the server goes down we don't want our server waiting for the data since we process a lot of data and we don't want it bogged down. To address this, we're trying to timeout our curl calls if they take more than a couple hundred milliseconds.

I found some documentation saying that CURLOPT_TIMEOUT_MS and CURLOPT_CONNECTTIMEOUT_MS should be available in my version of php and libcurl, but it does not seem to be timing out, even if I set the timeout to 1ms.

$url = "http://www.cnn.com;

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
   curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1);

   $data = curl_exec($ch);

   curl_close($ch);

Does anyone know what we're doing wrong or another way to do this?

+1  A: 

saw this in unresponsive dns server and curl multi timeouts not working:

"...We have had some times where a site that we pull information has had dns server become unresponsive. When this happens the timeouts set in curl (php bindings) do not work as expected. It times out after 1min 14 sec with "Could not resolve host: www.yahoo.com (Domain name not found)" To make this happen in test env we modify /etc/resolv.conf to have a nameserver that does not exist (nameserver 1.1.1.1). No mater what they are set at

(CURLOPT_CONNECTTIMEOUT, CURLOPT_CONNECTTIMEOUT_MS 
, CURLOPT_TIMEOUT, CURLOPT_TIMEOUT_MS)

they don't timeout when we cant get to the DNS server. I use curl_multi because i we have multiple sources that we pull info from at the same time. The example below makes one call for example simplicity. And as a side note curl_errno does not return an error code even though there was an error. Not sure why..."

pageman