我在CentOS虚拟机上运行MacOS X,任何cURL请求都需要15秒才能运行:
$c = curl_init('https://graph.facebook.com');
curl_exec($c); // takes 15s to return...
echo curl_getinfo($c, CURLINFO_NAMELOOKUP_TIME); // 15.01 seconds
然而,gethostbyname()非常快:
echo gethostbyname('graph.facebook.com'); // almost instant
而且,ping
几乎立即解析了这个名称。
默认情况下,/etc/resolv.conf
中只有nameserver 192.168.1.1
,因此我将其更改为使用Google服务器:
nameserver 8.8.8.8
nameserver 8.8.4.4
但运气不好。有什么暗示吗?
更新1:下面解决了这个问题:
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
因此,据我所知,它试图同时解决IPv4和IPv6问题,而IPv6分辨率在超时15秒后就会失败。
是因为Linux机器上的错误配置吗?
更新2
dig graph.facebook.com aaaa
;; reply from unexpected source: 10.0.2.2#53, expected 192.168.1.1#53
;; reply from unexpected source: 10.0.2.2#60944, expected 192.168.1.1#53
;; reply from unexpected source: 10.0.2.2#53, expected 192.168.1.1#53
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.4 <<>> graph.facebook.com aaaa
;; global options: +cmd
;; connection timed out; no servers could be reached
发布于 2013-07-23 08:46:15
问题是我的机器上的IPv6查找失败了。解决办法:
将/etc/resolv.conf
更改为:
nameserver 8.8.8.8
nameserver 8.8.4.4
重新启动后,resolv.conf
被覆盖,因此将这一行添加到/etc/sysconfig/network-scripts/ifcfg-eth0
(使用BOOTPROTO=dhcp
)解决了这个问题:
PEERDNS=no
现在一切都像一种魅力。
作为另一种选择,如果您在无法更改配置的服务器上遇到此问题,请以下列方式配置cURL:
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
发布于 2014-02-24 20:47:46
你的系统可能有问题,但我找到了解决问题的方法。
$urldata = parse_url($yourUrl);
$host = $urldata['host'];
$ip = gethostbyname($host);
$new_Url_dns_resolved = str_replace($host,$ip,$yourUrl);
//call the dns resolved url instead of the original url
$c = curl_init($new_Url_dns_resolved);
https://stackoverflow.com/questions/17814925
复制