Adding Methods to the Query Processor in PHP - Reviewing previous PHP networking functions: a quick look at the "QueryProcessor" class (
Page 2 of 5 )
Before I proceed to add some additional methods to the PHP 5 "QueryProcessor" class that you saw previously, allow me first to list its source code, as it was defined originally in the first article. Here is the signature for the "QueryProcessor" class:
class QueryProcessor{
private $host;
private $services=array
('http','https','ftp','telnet','imap','smtp','nicname','gopher',
'finger','pop3','www');
private $ports=array(21,23,25,43,70,79,80,110,143,443);
public function __construct($host='myhost.com'){
$this->host=$host;
}
// get IP address
public function getIp(){
if(!$ip=gethostbyname($this->host)){
throw new Exception('Error resolving host IP
address.');
}
return $ip;
}
// get list of IP addresses
public function getIpList(){
if(!$ips=implode(' - ',gethostbynamel($this->host))){
throw new Exception('Error getting list of IP
addresses for the provided hostname.');
}
return $ips;
}
// get host name
public function getHost(){
if(!$host=gethostbyaddr($this->getIp())){
throw new Exception('Error resolving host name.');
}
return $host;
}
// get TCP ports of Internet services
public function getServicePorts(){
$output='Retrieving services ports...Please wait.<br />';
foreach($this->services as $service){
if(!$port=getservbyname($service,'tcp')){
$output.='Error retrieving port of service
'.$service.'<br />';
}
else{
$output.='Service '.$service. ' runs on TCP
port :'. $port.'<br />';
}
}
return $output;
}
// get Services by TCP ports
public function getServiceNames(){
$output='Retrieving services names...Please wait.<br />';
foreach($this->ports as $port){
if(!$service=getservbyport($port,'tcp')){
$output.='Error retrieving service name on port
'.$port.'<br />';
}
else{
$output.='TCP Port '.$port. ' is used by
service :'. $service.'<br />';
}
}
return $output;
}
}
As you can see, the "QueryProcessor" class has a few handy methods, which use some of the most common PHP networking functions. With regards to this, the class is capable of resolving a specific host name to its corresponding IP address(or eventually a list of IP addresses), as well as performing the inverse process, that is given an IP address, turning it to the respective Internet host.
In addition, the query processor exposes two extra methods. The first one is focused on retrieving the number of the TCP port that corresponds to a specific service, while the second one performs the inverse process, obtaining the name of a service provided at a given TCP port.
Having covered at a glance how the "QueryProcessor" class was originally defined, here is an example that shows how to use its methods:
try{
// instantiate 'QueryProcessor' object
$queryProc=new QueryProcessor('hotmail.com');
// display host IP address
//echo $queryProc->getIp(); // displays 64.4.32.7
// display host name
//echo $queryProc->getHost(); // displays 'hotmail.com.br'
// display IP list
//echo $queryProc->getIpList(); // displays 64.4.32.7 -
64.4.33.7
// display services ports
echo $queryProc->getServicePorts();
/* displays the following list
Retrieving services ports...Please wait.
Service http runs on TCP port :80
Service https runs on TCP port :443
Service ftp runs on TCP port :21
Service telnet runs on TCP port :23
Service imap runs on TCP port :143
Service smtp runs on TCP port :25
Service nicname runs on TCP port :43
Service gopher runs on TCP port :70
Service finger runs on TCP port :79
Service pop3 runs on TCP port :110
Service www runs on TCP port :80
*/
// display service names
echo $queryProc->getServiceNames();
/* displays the following list
Retrieving services names...Please wait.
TCP Port 21 is used by service :ftp
TCP Port 23 is used by service :telnet
TCP Port 25 is used by service :smtp
TCP Port 43 is used by service :nicname
TCP Port 70 is used by service :gopher
TCP Port 79 is used by service :finger
TCP Port 80 is used by service :http
TCP Port 110 is used by service :pop3
TCP Port 143 is used by service :imap
TCP Port 443 is used by service :https
*/
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
As shown in the above example, once an Internet host name has been passed to the class constructor, the subsequent calls to the respective methods demonstrate how to obtain the corresponding IP address for that host, or the full list of IP addresses. They also display the TCP ports associated with specific services and, inversely, show the service that runs on a given port.
As you can see, most of these class methods are very simple, but you can see how they can be used as the building blocks of larger networking PHP applications.
Now, it's time to continue expanding the existing functionality of the "QueryProcessor" class, so please click on the link below in order to learn how you can implement the popular "ping" and "ipconfing" Windows utilities as new class methods.