PHP cURL consistently taking 15s to resolve DNS

curl, php

Solution

The problem was an IPv6 lookup failing on my machine. The solution:

Changed `/etc/resolv.conf` to:

nameserver 8.8.8.8
nameserver 8.8.4.4

After rebooting, `resolv.conf` got overwritten, so adding this line to `/etc/sysconfig/network-scripts/ifcfg-eth0` (which was using `BOOTPROTO=dhcp`) fixed the problem:

PEERDNS=no

And everything now works like a charm.

As an alternative, if you experience this problem on a server on which you can not change the configuration, configure cURL this way:

curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

Problem

I'm running PHP on a CentOS virtual machine under MacOS X, and any cURL request consistently takes 15s to run: ``` $c = curl_init('https://graph.facebook.com'); curl_exec($c); // takes 15s to return... echo curl_getinfo($c, CURLINFO_NAMELOOKUP_TIME); // 15.01 seconds ``` However, gethostbyname() is very fast: ``` echo gethostbyname('graph.facebook.com'); // almost instant ``` And, `ping` resolves the name almost instantly as well. By default, `/etc/resolv.conf` only had `nameserver 192.168.1.1` in it, so I changed it to use the Google DNS servers: ``` nameserver 8.8.8.8 nameserver 8.8.4.4 ``` But with no luck. Any hints? Update 1: The following fixes the problem: ``` curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); ``` So as far as I understand it, it's trying to resolve both IPv4 and IPv6, and IPv6 resolution fails, after a timeout of 15s. It that because of a misconfiguration on the Linux machine? Update 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 ```

Original source