如何使用PHP将主机名解析为IP地址,但使用不同的名称服务器(例如OpenDNS或Google Public DNS).
它似乎不能dns_get_record()或gethostbyname()不能使用与当前在系统上设置的名称服务器不同的名称服务器(在TCP/IP设置或中/etc/resolv.conf).
我发现的唯一方法是使用PEAR类Net/DNS,但它在PHP 5.4下给了我很多警告
Nic*_*ick 10
<?
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;
Run Code Online (Sandbox Code Playgroud)
如果允许从脚本运行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);
Run Code Online (Sandbox Code Playgroud)
注意:使用escapeshellargif $host和$dns来自用户输入.