Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
How I can resolve hostname to IP address using PHP,
but using different nameserver (eg.
OpenDNS
or
Google 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
).
The only way I've found is using PEAR class Net/DNS, but it gives me lots of warnings under PHP 5.4
–
–
–
–
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.
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.