Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl namelookup_time of 10 seconds

Tags:

php

curl

When executing a basic curl request in PHP to a plain text web page (http://whatismyip.org/) it takes more than 10 seconds to respond.

After looking at the info from curl is tells me that the namelookup_time is 10 seconds. I can see the exact same result when executing curl from the command line (Terminal).

Why does it take so long for the name lookup, from what I've read it's more than likely something relating to the server/my computer from which the PHP file is hosted.


Here's my code:

$ch = curl_init();  
curl_setopt( $ch, CURLOPT_URL, "whatismyip.org");
curl_exec( $ch );

$ci = curl_getinfo($ch);
print_r($ci);

Here's the info:

[url] => HTTP://whatismyip.org 
[content_type] => text/plain 
[http_code] => 200 
[header_size] => 45 
[request_size] => 53 
[filetime] => -1 
[ssl_verify_result] => 0 
[redirect_count] => 0 
[total_time] => 10.549943 
[namelookup_time] => 10.100938 
[connect_time] => 10.300077 
[pretransfer_time] => 10.300079 
[size_upload] => 0 
[size_download] => 14 
[speed_download] => 1 
[speed_upload] => 0 
[download_content_length] => -1 
[upload_content_length] => 0 
[starttransfer_time] => 10.549919 
[redirect_time] => 0 
[certinfo] => Array ( )
like image 598
Joshua Avatar asked Jan 03 '12 12:01

Joshua


3 Answers

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

Solved the problem for me. IPV6 got an obscure bug.

like image 150
Rodolphe Gerber Avatar answered Nov 20 '22 21:11

Rodolphe Gerber


I can't repeat this using exactly the code above - namelookup_time on my (Windows) machine comes back as 0, with total_time being ~0.5. namelookup_time is the time the OS has taken to resolve the DNS name for whatismyip.org, so you need to examine your server's DNS configuration.

At a guess, your configured primary DNS server doesn't exist/doesn't work, and the timeout is 10 seconds. This means that the OS will wait 10 seconds trying to contact the primary DNS, and when this times out falls through to the secondary, which works.

What are your configured DNS server(s)? Try using 8.8.8.8 (Google) as your primary DNS, if needed.

As a side note, it is best to supply a full URL to cURL, so use http://whatismyip.org/ instead of just whatismyip.org - although this does not seem to be the cause of this specific problem.

like image 31
DaveRandom Avatar answered Nov 20 '22 21:11

DaveRandom


Probably one of your DNS servers aren't replying in a timely fashion. Try this command for all IP's listed in /etc/resolv.conf:

dig @IP.TO.DNS.SERVER google.com

If I am correct, one of your DNS servers are not responding.

like image 3
jakobbg Avatar answered Nov 20 '22 21:11

jakobbg