Home >Backend Development >PHP Tutorial >curl_exec and wget execute too slowly, IPv6 causes trouble_PHP tutorial
When I used the curl function of php today, I found that it took about 5 seconds to get the result, which was really too slow. If you access the same URL using a browser, you can get the page immediately. Later, I discovered that even without using php, using the native command wget under Linux to obtain web pages is very slow. This is really strange. It seems that it is not a problem with the program, but a problem with the network settings.
When executing wget, it can be clearly seen that the blocking occurs in the DNS domain name resolution part.
$ wget www.myproject.com
--2012-06-18 12:17:30-- http://www.myproject.com/
Resolving www.myproject.com... # Stalled here for about 5 seconds
192.168.1.187
Connecting to www.myproject.com|192.168.1.187|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: index.html?
[<=>
2012-06-18 12:17:35 (264 MB/s) - index.html saved [5200]
I am using a development environment and the domain name resolves to an internal IP. The strange thing is that when I ping www.myproject.com I quickly get an IP address and get an ICMP message back. Why is wget's DNS resolution so slow? www.2cto.com
Many people have asked this question on Google and StackOverflow. Some said it was a problem with reverse DNS reverse domain name resolution, and some said that the domain name should be added to the host file... none of them solved my problem. When I was puzzled, I suddenly remembered that last week was IPv6 Day. Our operation and maintenance sent an email saying that IPv6 had been enabled on all company networks. Could this be the reason? After following this clue to investigate, sure enough, it suddenly became clear.
$ wget -4 www.myproject.com
Results were returned instantly. It seems that the operation and maintenance did not help us bind an IPv6 address to this domain name. After the entire network is upgraded, IPv6 will be resolved first by default. If the domain does not have IPv6, it will wait for the IPv6 resolution failure timeout before proceeding as before. The process goes to IPv4.
For PHP curl, you only need to add the following sentence to solve the delay problem:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
Nowadays, many servers have IPv6 turned on but have no routing and cannot really work. Instead, it causes some unpredictable problems. I deeply understand that IPv6 has a long road ahead...