当在PHP中执行对纯文本网页(http://whatismyip.org/)的基本curl请求时,需要超过10秒才能响应。
查看curl的信息后,它告诉我namelookup_time是10秒。当从命令行(终端)执行curl时,我可以看到完全相同的结果。
为什么要花这么长时间来查找,从我所读到的,它很可能与PHP文件托管的服务器/我的计算机有关。
下面是我的代码:
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "whatismyip.org");
curl_exec( $ch );
$ci = curl_getinfo($ch);
print_r($ci);
信息如下:
[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 ( )
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
为我解决了这个问题。IPV6有一个模糊的错误
我不能完全使用上面的代码重复这一点- namelookup_time
在我的(Windows)机器上返回为0
, total_time
为~ 0.5
。namelookup_time
是操作系统为whatismyip.org
解析DNS名称所花费的时间,因此您需要检查服务器的DNS配置。
猜测,您配置的主DNS服务器不存在/不工作,超时时间为10秒。这意味着操作系统将等待10秒来尝试联系主DNS,当此超时时,将切换到辅助DNS,这可以工作。
您配置的DNS服务器是什么?如果需要,请尝试使用8.8.8.8
(Google)作为您的主DNS。
作为一个边注,最好提供一个完整的 URL给cURL,所以使用http://whatismyip.org/
而不是whatismyip.org
-尽管这似乎不是造成这个特定问题的原因。
可能是您的某个DNS服务器没有及时回复。对/etc/resolv.conf中列出的所有IP试试这个命令:
dig @IP.TO.DNS.SERVER google.com
如果我是正确的,你的一个DNS服务器没有响应。