Whois example with php.
To make a whois query of a domain name using the php language, you can use the fsockopen, function, which allows you to open a socket connection on a specific portport 43 is the whois server port) as if you were using a file.
In the following example we use a map (extension -> server) stored in the vector $mappa_estensione_server which associates a specific domain extension with its whois server. Here are the whois servers for some gTLDs and ccTLDs .com,.net,.org,.info,.biz, but you only need to add a new pair to the map to handle a new extension.
whois.php
<?php
class whois_search {
var $mappa_estensione_server = array (
"it" => "whois.nic.it",
"com" => "whois.internic.net",
"net" => "whois.internic.net",
"org" => "whois.publicinterestregistry.net",
"info" => "whois.afilias.net",
"biz" => "whois.neulevel.biz",
"eu" => "whois.registry.eu",
"name" => "whois.nic.name",
"mobi" => "whois.dotmobiregistry.net",
"us" => "whois.nic.us",
"me" => "whois.meregistry.net",
"tv" => "whois.nic.tv",
"ws" => "whois.nic.ws",
"cn" => "whois.cnnic.cn"
);
function do_whois($dominio) {
$dominio = strtolower(trim($dominio));
$pos_punto = strrpos($dominio, ".");
if (!$pos_punto) {
return "nome di dominio non valido";
} else {
$estensione = substr($dominio, $pos_punto + 1);
if (!array_key_exists($estensione,$this->mappa_estensione_server)) {
return "estensione <b><i>.".$estensione."</i></b> non supportata";
}
}
$server = $this->mappa_estensione_server[$estensione];
$puntatore_whois = fsockopen($server, 43, $errno, $errstr, 30);
$html_output = '';
if (!$puntatore_whois) {
$html_output = "$errstr ($errno)";
} else {
fputs($puntatore_whois, "$dominio\r\n");
$html_output .= "<pre>\r\n";
while (!feof($puntatore_whois)) {
$html_output .= fread($puntatore_whois,128);
}
$html_output .= "</pre>";
fclose ($puntatore_whois);
}
return $html_output;
}
function print_allowed_extension () {
$vettore_estensioni = array_keys($this->mappa_estensione_server);
$estensioni_supportate = '';
for ($i = 0; $i < count($vettore_estensioni); $i++) {
$estensioni_supportate .= ' .'.$vettore_estensioni [$i].' ';
}
return $estensioni_supportate;
}
}
$whois = new whois_search();
$html_form =
'<form method=post action="">
Inserisci il nome di dominio: <input type="text" name="dominio" size="40">
<br>
<small>estensioni suppoortate: '.$whois->print_allowed_extension ().'</small>
<br>
<input type="submit" value="invio"> <input type="reset" value="cancella">
</form>';
if (isset($_POST["dominio"])) {
$corpo_pagina = '<h2>Domain name Whois</h2>'.$html_form.'<hr>';
$corpo_pagina .= '<h2>Risultato Whois per il dominio : '.$_POST["dominio"].'</h2><br>';
$corpo_pagina .= $whois->do_whois($_POST["dominio"]);
} else {
$corpo_pagina = '<h2>Domain name Whois</h2>'.$html_form;
}
unset($whois);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<head>
<title>Ricerca Whois</title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<?=$corpo_pagina?>
</body>
</html>
The script is extremely simple. The central method is do_whois which, once it has found the correct whois server, opens a socket connection to it on port 43 using fsockopen. he open connection then waits for a domain name, which is sent using the fputs function. The result is read by the fgets unction up to the end-of-file character and returned as a string as the result of the do_whois method.
There is only one other method in the whois_search class, print_allowed_extension, which returns a string of valid domain extensions and which can be printed on the screen to guide the user.
This is of course only a basic example, but since the whois function is enclosed in a class, it is possible to extend the object according to one's needs by adding properties and methods, such as searching on name servers or handlers for .it domains.