Posted By

apphp-snippets on 05/22/17


Tagged

php whois


Versions (?)

WhoIs Query in PHP


 / Published in: PHP
 

URL: www.apphp.com/index.php?snippet=php-whois-query

This code allows to perform a query to WhoIs service in PHP.

  1. <?php
  2. function whois_query($domain) {
  3. // fix the domain name: $domain = strtolower(trim($domain));
  4. $domain = preg_replace('/^http:\/\//i', '', $domain);
  5. $domain = preg_replace('/^www\./i', '', $domain);
  6. $domain = explode('/', $domain);
  7. $domain = trim($domain[0]);
  8. // split the TLD from domain name
  9. $_domain = explode('.', $domain);
  10. $lst = count($_domain)-1;
  11. $ext = $_domain[$lst];
  12. // You find resources and lists
  13. // like these on wikipedia:
  14. //
  15. // de.wikipedia.org/wiki/Whois
  16. //
  17. $servers = array(
  18. "biz" => "whois.neulevel.biz",
  19. "com" => "whois.internic.net", "us" => "whois.nic.us",
  20. "coop" => "whois.nic.coop", "info" => "whois.nic.info",
  21. "name" => "whois.nic.name", "net" => "whois.internic.net",
  22. "gov" => "whois.nic.gov", "edu" => "whois.internic.net",
  23. "mil" => "rs.internic.net", "int" => "whois.iana.org",
  24. "ac" => "whois.nic.ac", "ae" => "whois.uaenic.ae",
  25. "at" => "whois.ripe.net", "au" => "whois.aunic.net",
  26. "be" => "whois.dns.be", "bg" => "whois.ripe.net",
  27. "br" => "whois.registro.br", "bz" => "whois.belizenic.bz",
  28. "ca" => "whois.cira.ca", "cc" => "whois.nic.cc",
  29. "ch" => "whois.nic.ch", "cl" => "whois.nic.cl",
  30. "cn" => "whois.cnnic.net.cn", "cz" => "whois.nic.cz",
  31. "de" => "whois.nic.de", "fr" => "whois.nic.fr",
  32. "hu" => "whois.nic.hu", "ie" => "whois.domainregistry.ie",
  33. "il" => "whois.isoc.org.il", "in" => "whois.ncst.ernet.in",
  34. "ir" => "whois.nic.ir", "mc" => "whois.ripe.net",
  35. "to" => "whois.tonic.to", "tv" => "whois.tv",
  36. "ru" => "whois.ripn.net", "org" => "whois.pir.org",
  37. "aero" => "whois.information.aero", "nl" => "whois.domain-registry.nl"
  38. );
  39.  
  40. if (!isset($servers[$ext])){ die('Error: No matching nic server found!'); }
  41. $nic_server = $servers[$ext];
  42. $output = '';
  43.  
  44. // connect to whois server:
  45. if ($conn = fsockopen ($nic_server, 43)) {
  46. fputs($conn, $domain."\r\n");
  47. while(!feof($conn)) {
  48. $output .= fgets($conn,128);
  49. }
  50. fclose($conn);
  51. }
  52. else {
  53. die('Error: Could not connect to ' . $nic_server . '!');
  54. }
  55. return $output;
  56. }
  57. ?>

Report this snippet  

You need to login to post a comment.