16

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

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

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

4

3 回答 3

11
<?
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;
于 2012-07-19T18:52:10.270 回答
8

试试net_dns2(它也在 PEAR 中)。

于 2012-07-19T16:50:26.717 回答
6

如果您被允许从您的脚本运行 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);

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

于 2012-07-19T18:26:47.327 回答