[PHP] Whois query using PHP


/ Published in: PHP
Save to your folder(s)

f you need to get the whois information for a specific domain, why not using PHP to do it? The following function take a domain name as a parameter, and then display the whois info related to the domain.


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

URL: http://www.jonasjohn.de/snippets/php/whois-query.htm

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.