17

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

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Nick
  • 9,962
  • 4
  • 42
  • 80
  • Is it an option to run a python script through exec and just read the shell output? I've seen this same problem solved in urllib2 on SO here:http://stackoverflow.com/questions/2236498/tell-urllib2-to-use-custom-dns/2237040#2237040 – DeaconDesperado Jul 19 '12 at 16:20
  • @DeaconDesperado: Why use a python script? If you can `exec` in PHP, why not just call the unix `host` command? `exec('host google.com 8.8.8.8')`? – gen_Eric Jul 19 '12 at 18:07
  • @Rocket, just suggesting what I was familiar with, hence the comment rather than answer ;) – DeaconDesperado Jul 19 '12 at 18:12
  • good choice, but I want it to be fast. exec() actually forks and uses pipes to redirects the stdin and stdout. This is quite costly operation and in my case will be waste of CPU resources. – Nick Jul 19 '12 at 18:44

3 Answers3

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;
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Nick
  • 9,962
  • 4
  • 42
  • 80
  • It is possible if the IP of a visitor to my web. Whether the visitor uses the DNS to Google? – jcarlosweb Dec 29 '15 at 21:26
  • 1
    yes, but you do not need to use dns2 for this. apache /php gives you $_SERVER[REMOTE_ADDR] for this. If unsure - do print_r($_SERVER) or ask question about it. – Nick Dec 30 '15 at 06:13
  • Excuse me. To know if the visitor of my web using google public dns? – jcarlosweb Dec 30 '15 at 17:25
8

Try net_dns2 (it's also in PEAR).

Sandman4
  • 2,673
  • 2
  • 22
  • 18
  • looks promising, will give you feedback soon – Nick Jul 19 '12 at 18:42
  • require_once 'Net/DNS2.php'; $resolver = new Net_DNS2_Resolver( array('nameservers' => array('8.8.8.8')) ); $resp = $resolver->query("hooktube.com.", 'A'); print_r($resp); echo $resp->answer[0]->address; – Nick Jul 19 '12 at 18:50
  • [https://netdns2.com/documentation/examples/](https://netdns2.com/documentation/examples/) – a55 Feb 01 '21 at 18:00
7

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.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • @jcarlosweb: I'm sorry, but I can't understand what you are asking. – gen_Eric Dec 29 '15 at 21:45
  • Excuse me. To know if the visitor of my web using google public dns? – jcarlosweb Dec 30 '15 at 15:34
  • 3
    There is no way to detect this. Why you need such information anyway? – Nick Dec 30 '15 at 22:23
  • I am getting these responses. Note that for yahoo it says: 'Addresses' C:\Users\User>nslookup slavi-2016-05-04.com 8.8.8.8 Server: google-public-dns-a.google.com Address: 8.8.8.8 *** google-public-dns-a.google.com can't find slavi-2016-05-04.com: Non-existent domain C:\Users\User>nslookup yahoo.com 8.8.8.8 Server: google-public-dns-a.google.com Address: 8.8.8.8 Non-authoritative answer: Name: yahoo.com Addresses: 2001:4998:58:c02::a9 2001:4998:c:a06::2:4008 2001:4998:44:204::a7 98.138.253.109 98.139.183.24 206.190.36.45 – Svetoslav Marinov May 04 '16 at 14:17