从DNS获取IP而不使用gethostbyname? [英] Get IP from DNS without using gethostbyname?
问题描述
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.
那么有没有办法使用套接字进行DNS查询cURL代替?我能够使用保存服务器资源的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('/\nAddress: (.*)\n/', $query, $matches))
return trim($matches[1]);
return $host;
}
?>
http://www.php.net/manual/en/function.gethostbyname.php#92870
这篇关于从DNS获取IP而不使用gethostbyname?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!