Get IP from DNS without using gethostbyname?

你离开我真会死。 提交于 2019-12-19 09:07:04

问题


PHP's gethostbyname() function doesn't seem to be configurable as far as timeouts are concerned. I want to limit DNS requests to 1 second or less as anymore shows a problem and should be ignored.

So, is there a way to make a DNS query using sockets or cURL instead? I was able to make SMTP requests using PHP streams which saved server resources, so I'm looking to do the same with DNS queries.


回答1:


The comments on the PHP article have some options for setting/including a timeout:

<?php
function getAddrByHost($host, $timeout = 3) {
   $query = `nslookup -timeout=$timeout -retry=1 $host`;
   if(preg_match('/\nAddress: (.*)\n/', $query, $matches))
      return trim($matches[1]);
   return $host;
}
?>

http://www.php.net/manual/en/function.gethostbyname.php#92870



来源:https://stackoverflow.com/questions/4305604/get-ip-from-dns-without-using-gethostbyname

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!