Recently used gethostbyname, in the local environment Mac+apache execution speed very fast, fraction seconds, but put on the server side is not, server-side environment Lnmp, execution time is more than 5 seconds, how to solve this problem?
Besides gethostbyname, is there any other way to resolve the domain name to IP?
Reply content:
Recently used gethostbyname, in the local environment Mac+apache execution speed very fast, fraction seconds, but put on the server side is not, server-side environment Lnmp, execution time is more than 5 seconds, how to solve this problem?
Besides gethostbyname, is there any other way to resolve the domain name to IP?
gethostbyname requires DNS resolution, which typically takes 1-5 seconds.
It is recommended to change to another method.
function Gethostbyname2 ($host, $timeout = 3) { $query = ' nslookup-timeout= $timeout-retry=1 $host '; if (Preg_match ('/\naddress: (. *) \n/', $query, $matches)) return Trim ($matches [1]); return $host;}
Try your @april.l the code seems wrong. Modified a bit, and do not know how to do, gethostbyname the speed of execution suddenly came up, under 1s clock, I seem to have nothing changed, just slept a day. I don't know how.
function Gethostbyname2 ($host, $timeout = 3) {$query = ' nslookup-timeout= '. $timeout. '-retry=1 '. $host; $query = Shell_exe C ($query); if (Preg_match ('/\naddress: (. *) \n/', $query, $matches)) return trim ($matches [1]); return $host;}
PHP gethostbyname does not have any special logic, in most cases, it is directly called gethostbyname (3), and DNS parsing and your network environment is very relevant, so this is really not good generalize:)
The easy way is to install DNSMASQ on a machine in the local or LAN, and then refer to DNS in the past, as a DNS query acceleration, it will be much faster.