An example Prefix WhoIs client in PHP
<?php
/**
*
* Prefix WhoIs Bulk Query Interface
*
* -- a native interface to Prefix WhoIs implemented in PHP*
*
* * requires PHP >= 5
*
* Simply call doPWLookupBulk(array $queryArray) and it will
* return an associative array of AS numbers (and other data
* indexed by the IP addresses passed to it in the $queryArray
* argument.
* -- by Victor Oppleman (2005)
*/
function doPWLookupBulk($queryarray) {
# Initialization
$pwserver = 'whois.pwhois.org'; # Prefix WhoIswhois Server (public)
$pwport = 43; # Port to which Prefix WhoIswhois listens
$socket_timeout = 20; # Timeout for socket connection operations
$socket_delay = 5; # Timeout for socket read/write operations
$max_batch = 500; # Maximum retrieval batch size (n per batch)
$app_name = "PWPHPLib"; # Library name
# Mostly generic code beyond this point
$pwserver = gethostbyname($pwserver);
$current_batch = 0;
$buffer = ''; # Temporary buffer space
$response = array(); # The response array set of answers
# Optimize query array and renumber
$queryarray = array_unique($queryarray);
$numvals = 0;
foreach ($queryarray as $a) {
if(filter_var($a, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$qarray[$numvals] = $a;
$numvals++;
}
}
# Build, then submit batches of bulk queries
while ($current_batch < $numvals) {
$range = $current_batch + $max_batch;
if ($numvals < $range) $range = $numvals;
$batch_range = $current_batch . ".." . $range;
#print "Processing batch $batch_range of $numvals\n";
$request = "begin\napp=\"$app_name\"\n";
while ($current_batch < $numvals) {
$request .= $qarray[$current_batch] . "\n";
$current_batch++;
if ($current_batch % $max_batch == 0) break;
}
$request .= "end\n";
# Create a new socket
$sock = stream_socket_client("tcp://".$pwserver.":".$pwport,
$errno, $errstr, $socket_timeout);
if (!$sock) {
#echo "$errstr ($errno)<br />\n";
return 0;
}
stream_set_blocking($sock,0); # Set stream to non-blocking
stream_set_timeout($sock, $socket_delay); # Set stream delay timeout
fwrite($sock, $request);
# Keep looking for more responses until EOF or timeout
$before_query = date('U');
while(!feof($sock)){
if($buf=fgets($sock,128)){
$buffer .= $buf;
if (date('U') > ($before_query + $socket_timeout)) break;
}
}
fclose($sock);
#print "REQUEST:\n\n$request\n\nRESPONSE:\n\n$buffer";
$resp = explode("\n",$buffer);
$entity_id = 0; $found = 0;
foreach ($resp as $r) {
$matcher = '';
if (stristr($r,':')) {
if (stristr($r,'ip')) {
if ($found > 0) { $entity_id++; $found = 0; }
$found++;
}
$matcher = explode(":",$r);
$matcher[0] = preg_replace('/[^A-Za-z0-9]/', '', $matcher[0]);
$matcher[1] = preg_replace('/[^A-Za-z0-9-\n\s_\.\/,;]/', '_', $matcher[1]);
$response[$qarray[$entity_id]][strtolower($matcher[0])] = trim($matcher[1]);
}
if ($entity_id >= array_count_values($qarray)) break;
}
}
return $response;
}
function doPWLookup($query) {
# Initialization
$pwserver = 'whois.pwhois.org'; # Prefix WhoIswhois Server (public)
$pwport = 43; # Port to which Prefix WhoIswhois listens
$socket_timeout = 20; # Timeout for socket connection operations
$socket_delay = 5; # Timeout for socket read/write operations
$max_batch = 500; # Maximum retrieval batch size (n per batch)
$app_name = "PWPHPLib"; # Library name
# Mostly generic code beyond this point
$pwserver = gethostbyname($pwserver);
$buffer = ''; # Temporary buffer space
$response = array(); # The response array set of answers
$query = trim($query);
if (!filter_var($query, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return 0;
}
# Build, then submit query
$request = "app=\"$app_name\"\n";
$request .= $query . "\n";
# Create a new socket
$sock = stream_socket_client("tcp://".$pwserver.":".$pwport,
$errno, $errstr, $socket_timeout);
if (!$sock) {
#echo "$errstr ($errno)<br />\n";
return 0;
}
stream_set_blocking($sock,0); # Set stream to non-blocking
stream_set_timeout($sock, $socket_delay); # Set stream delay timeout
fwrite($sock, $request);
# Keep looking for more responses until EOF or timeout
$before_query = date('U');
while(!feof($sock)){
if($buf=fgets($sock,128)){
$buffer .= $buf;
if (date('U') > ($before_query + $socket_timeout)) break;
}
}
fclose($sock);
#print "REQUEST:\n\n$request\n\nRESPONSE:\n\n$buffer";
$resp = explode("\n",$buffer);
$entity_id = 0; $found = 0;
foreach ($resp as $r) {
$matcher = '';
if (stristr($r,':')) {
if (stristr($r,'ip')) {
if ($found > 0) { $entity_id++; $found = 0; }
$found++;
}
$matcher = explode(":",$r);
$matcher[0] = preg_replace('/[^A-Za-z0-9]/', '', $matcher[0]);
$matcher[1] = preg_replace('/[^A-Za-z0-9-\n\s_\.\/,;]/', '_', $matcher[1]);
$response[strtolower($matcher[0])] = trim($matcher[1]);
}
}
return $response;
}
### An example of calling the functions... and the results they return:
$test_array = array('4.2.2.1','12.0.0.0');
if (!($pwresp = doPWLookupBulk($test_array))) {
print "<h2>Your query wasn't answered.</h2>\n";
exit();
}
print "<pre>";
foreach ($pwresp as $ip => $resp) {
print "IP: " . $ip . "<br />";
print_r($resp);
print "<br />";
}
print "</pre>";
if (!($pwresp = doPWLookup("4.2.2.1"))) {
print "<h2>Your query wasn't answered.</h2>\n";
exit();
}
print "<pre>";
print_r($pwresp);
print "</pre>";
?>
This produces the following output:
IP: 4.2.2.1
Array ( [ip] => 4.2.2.1 [originas] => 3356 [prefix] => 4.0.0.0/9 [aspath] => 3292 3356 [asorgname] => Level 3 Communications [orgname] => Level 3 Communications, Inc. [netname] => LVLT-ORG-4-8 [cachedate] => 1280551579 [latitude] => 39.913500 [longitude] => -105.093000 [city] => BROOMFIELD [region] => COLORADO [country] => UNITED STATES )
IP: 12.0.0.0
Array ( [ip] => 12.0.0.0 [originas] => 7018 [prefix] => 12.0.0.0/9 [aspath] => 209 7018 [asorgname] => AT_T WorldNet Services [orgname] => ATT LINCROFT ORT [netname] => ATT-LIN850-0 [cachedate] => 1280551579 [latitude] => 40.590200 [longitude] => -74.147100 [city] => STATEN ISLAND [region] => NEW YORK [country] => UNITED STATES )
Array
(
[ip] => 4.2.2.1
[originas] => 3356
[prefix] => 4.0.0.0/9
[aspath] => 3292 3356
[asorgname] => Level 3 Communications
[orgname] => Level 3 Communications, Inc.
[netname] => LVLT-ORG-4-8
[cachedate] => 1280551579
[latitude] => 39.913500
[longitude] => -105.093000
[city] => BROOMFIELD
[region] => COLORADO
[country] => UNITED STATES
)