PHP cURL一直需要15秒才能解析DNS

Ben*_*min 7 php curl

我在MacOS X下的CentOS虚拟机上运行PHP,并且任何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
Run Code Online (Sandbox Code Playgroud)

但是,gethostbyname()非常快:

echo gethostbyname('graph.facebook.com'); // almost instant
Run Code Online (Sandbox Code Playgroud)

并且,ping几乎立即解决了这个名字.

默认情况下,/etc/resolv.conf只有nameserver 192.168.1.1其中,所以我将其更改为使用Google DNS服务器:

nameserver 8.8.8.8
nameserver 8.8.4.4
Run Code Online (Sandbox Code Playgroud)

但没有运气.任何提示?


更新1:以下修复了此问题:

curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
Run Code Online (Sandbox Code Playgroud)

因此,据我所知,它正在尝试解决IPv4和IPv6,并且在15秒超时后IPv6解析失败.

它是因为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
Run Code Online (Sandbox Code Playgroud)

Ben*_*min 14

问题是我的机器上的IPv6查找失败.解决方案:

改为/etc/resolv.conf:

nameserver 8.8.8.8
nameserver 8.8.4.4
Run Code Online (Sandbox Code Playgroud)

重新启动后,resolv.conf被覆盖,所以添加此行/etc/sysconfig/network-scripts/ifcfg-eth0(正在使用BOOTPROTO=dhcp)修复了问题:

PEERDNS=no
Run Code Online (Sandbox Code Playgroud)

现在一切都像魅力一样.

或者,如果您在无法更改配置的服务器上遇到此问题,请以这种方式配置cURL:

curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
Run Code Online (Sandbox Code Playgroud)