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) {

$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)


# 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($aFILTER_VALIDATE_IPFILTER_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\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 "<pre>REQUEST:\n\n$request\n\nRESPONSE:\n\n$buffer</pre>";
    
      
$resp explode("\n",$buffer);
      
$entity_id 0$found 0;
      foreach (
$resp as $r) {
        unset(
$matcher);
    
        if (
stristr($r,':')) {
           if (
preg_match('/^IP:/i',$r) > 0) {
                if (
$found 0) { $entity_id++; $found 0; }
                
$found++;
           }
           
$r html_entity_decode(strip_tags(trim($r)));
           
$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])] = $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($queryFILTER_VALIDATE_IPFILTER_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) {
    unset(
$matcher);
    
    
$r html_entity_decode(strip_tags(trim($r)));
    if (
stristr($r,':')) {
       if (
preg_match('/^IP:/i',$r) > 0) {
            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] => 8492 20485 3356 [asorgname] => Level 3 Communications, Inc. [orgname] => Level 3 Communications, Inc. [netname] => LVLT-ORG-4-8 [cachedate] => 1409402962 [latitude] => 39.882822 [longitude] => -105.106477 [city] => BROOMFIELD [region] => COLORADO [country] => UNITED STATES [countrycode] => US )
IP: 12.0.0.0
Array ( [ip] => 12.0.0.0 [originas] => 7018 [prefix] => 12.0.0.0/9 [aspath] => 8492 9002 2914 7018 [asorgname] => AT&T Services, Inc. [orgname] => ATT LINCROFT ORT [netname] => ATT-LIN850-0 [cachedate] => 1409402962 [latitude] => 38.760780 [longitude] => -90.412860 [city] => BRIDGETON [region] => MISSOURI [country] => UNITED STATES [countrycode] => US )
Array
(
    [ip] => 4.2.2.1
    [originas] => 3356
    [prefix] => 4.0.0.0/9
    [aspath] => 8492 20485 3356
    [asorgname] => Level 3 Communications, Inc.
    [orgname] => Level 3 Communications, Inc.
    [netname] => LVLT-ORG-4-8
    [cachedate] => 1409402962
    [latitude] => 39.882822
    [longitude] => -105.106477
    [city] => BROOMFIELD
    [region] => COLORADO
    [country] => UNITED STATES
    [countrycode] => US
)

Currently Sep 1st 2014, 11:19pm GMT