Resolve hostname in PHP using different name server

让人想犯罪 __ 提交于 2019-12-03 11:41:53

Try net_dns2 (it's also in PEAR).

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;

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

$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 escapeshellarg if $host and $dns are from user input.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!