curl-and-php
unresponsive dns server and curl multi timeouts not working
Date: Tue, 3 Jun 2008 11:03:58 -0500
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
---------------------------------------------------------------------------------------------------------------
system info
PHP Version => 5.2.5
cURL Information => libcurl/7.17.0 OpenSSL/0.9.7a zlib/1.2.3 c-ares/1.4.0
Linux HOSTNAME 2.6.9-55.ELsmp #1 SMP Wed May 2 14:04:42 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux
---------------------------------------------------------------------------------------------------------------
<?php
$data_struct = array();
$options = array( CURLOPT_URL => 'http://www.yahoo.com/'
, CURLOPT_USERAGENT => 'agent bla'
, CURLOPT_REFERER => 'testing'
, CURLOPT_TIMEOUT => 1
, CURLOPT_CONNECTTIMEOUT => 1
// , CURLOPT_TIMEOUT_MS => 200
// , CURLOPT_CONNECTTIMEOUT_MS => 200
, CURLOPT_VERBOSE => 1
, CURLOPT_NOSIGNAL => true // false
, CURLOPT_NOPROGRESS => true
, CURLOPT_FAILONERROR => true
, CURLOPT_FRESH_CONNECT => true // false
, CURLOPT_FORBID_REUSE => true // false
, CURLOPT_SSL_VERIFYPEER => false
, CURLOPT_SSL_VERIFYHOST => false
, CURLOPT_RETURNTRANSFER => true
, CURLOPT_MAXREDIRS => 2
, CURLOPT_FOLLOWLOCATION => true
, CURLOPT_AUTOREFERER => true
// , CURLOPT_MAXCONNECTS => 10
// , CURLOPT_CLOSEPOLICY => CURLCLOSEPOLICY_OLDEST
// , CURLOPT_DNS_CACHE_TIMEOUT => 3
// , CURLOPT_DNS_USE_GLOBAL_CACHE => false
);
$data_struct[]['opts'] = $options;
//create the multiple cURL handle
$mh = curl_multi_init();
// create and add handles to data structure
foreach ($data_struct as $i => $data){
$data_struct[$i]['handle'] = curl_init();
curl_setopt_array($data_struct[$i]['handle'], $data_struct[$i]['opts']);
curl_multi_add_handle($mh, $data_struct[$i]['handle']);
}
do {
curl_multi_exec($mh,$running);
while ( FALSE !==($msg = curl_multi_info_read($mh)) ) {
// print "ding ". $msg['handle'] ."\n";
}
} while ($running > 0);
foreach ($data_struct as $i => $data){
$row = array();
$row['pretransfer_time'] = curl_getinfo($data_struct[$i]['handle'] , CURLINFO_PRETRANSFER_TIME);
$row['starttranser_time'] = curl_getinfo($data_struct[$i]['handle'] , CURLINFO_STARTTRANSFER_TIME);
$row['connect_time'] = curl_getinfo($data_struct[$i]['handle'] , CURLINFO_CONNECT_TIME);
$row['namelookup_time'] = curl_getinfo($data_struct[$i]['handle'] , CURLINFO_NAMELOOKUP_TIME);
$row['total_time'] = curl_getinfo($data_struct[$i]['handle'] , CURLINFO_TOTAL_TIME);
$row['redirect_time'] = curl_getinfo($data_struct[$i]['handle'] , CURLINFO_REDIRECT_TIME);
$row['redirect_count'] = curl_getinfo($data_struct[$i]['handle'] , CURLINFO_REDIRECT_COUNT);
$row['error'] = curl_error($data_struct[$i]['handle']);
$row['errorno'] = curl_errno($data_struct[$i]['handle']);
print_r($row);
curl_multi_remove_handle($mh, $data_struct[$i]['handle']);
curl_close($data_struct[$i]['handle']);
}
curl_multi_close($mh);
?>
_________________________________________________________________
Search that pays you back! Introducing Live Search cashback.
http://search.live.com/cashback/?&pkw=form=MIJAAF/publ=HMTGL/crea=srchpaysyouback
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2008-06-03