在不使用 gethostbyname 的情况下从 DNS 获取 IP?

2022-01-04 00:00:00 sockets php dns

PHP 的 gethostbyname() 函数似乎不可配置就超时而言.我想将 DNS 请求限制为 1 秒或更短,因为再次显示问题并且应该被忽略.

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.

那么,有没有办法使用套接字或 cURL 进行 DNS 查询?我能够使用节省服务器资源的 PHP 流发出 SMTP 请求,因此我希望对 DNS 查询执行相同的操作.

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.

推荐答案

的评论PHP 文章有一些设置/包括超时的选项:

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('/
Address: (.*)
/', $query, $matches))
      return trim($matches[1]);
   return $host;
}
?>

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

相关文章