使用不同的名称服务器解析 PHP 中的主机名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11563956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:46:37  来源:igfitidea点击:

Resolve hostname in PHP using different name server

phpdnsnslookup

提问by Nick

How I can resolve hostname to IP address using PHP, but using different nameserver (eg. OpenDNSor Google Public DNS).

我如何使用 PHP 将主机名解析为 IP 地址,但使用不同的名称服务器(例如OpenDNSGoogle Public DNS)。

It not seem that dns_get_record()or gethostbyname()are able to use a different nameserver than one currently set up on the system (in TCP/IP settings or in /etc/resolv.conf).

似乎无法dns_get_record()gethostbyname()无法使用与系统上当前设置的名称服务器不同的名称服务器(在 TCP/IP 设置中或在 中/etc/resolv.conf)。

The only way I've found is using PEAR class Net/DNS, but it gives me lots of warnings under PHP 5.4

我发现的唯一方法是使用 PEAR 类 Net/DNS,但它在 PHP 5.4 下给了我很多警告

采纳答案by Sandman4

Try net_dns2(it's also in PEAR).

试试net_dns2(它也在 PEAR 中)。

回答by Nick

<?
require_once 'Net/DNS2.php';

$resolver = new Net_DNS2_Resolver( array('nameservers' => array('208.67.222.123')) );

$resp = $resolver->query("hooktube.com.", 'A');

print_r($resp);

echo $resp->answer[0]->address;

回答by Rocket Hazmat

If you are allowed to run shell scripts from your script, you can use the system's nslookupcommand.

如果允许您从脚本运行 shell 脚本,则可以使用系统nslookup命令。

$host = 'stackoverflow.com';
$dns = '8.8.8.8';  // Google Public DNS

$ip = `nslookup $host $dns`; // the backticks execute the command in the shell

$ips = array();
if(preg_match_all('/Address: ((?:\d{1,3}\.){3}\d{1,3})/', $ip, $match) > 0){
    $ips = $match[1];
}

print_r($ips);

Note: use escapeshellargif $hostand $dnsare from user input.

注意:使用escapeshellargif$host$dnsare来自用户输入。