相关疑难解决方法(0)

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

我在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 …
Run Code Online (Sandbox Code Playgroud)

php curl

7
推荐指数
1
解决办法
8863
查看次数

标签 统计

curl ×1

php ×1