php gethostbyname 效率问题


最近用到了gethostbyname,在本地的环境Mac+Apache下执行速度非常快,零点几秒上,但是放到服务器端就不行了,服务器端环境 LNMP ,执行时间都在5秒钟以上,怎么才能解决这个问题?
除了gethostbyname,还有什么能将域名解析成ip的其他方法么?

php 效率

kula127 10 years, 8 months ago

gethostbyname 需要DNS解析,通常需要1-5秒。
建议换成其他方法。

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;
}
BC245 answered 10 years, 8 months ago

Your Answer