HomePHP Page 4 - Build a Query Processor Class for Networking in PHP 5
Assembling the methods: listing the full source code of the "QueryProcessor" class - PHP
Welcome to the first of three tutorials in a series that covers network programming in PHP. In this article you will learn how to use some useful PHP networking functions that will help you perform a variety of common networking tasks. To that end, you will build a simple networking query application in PHP 5.
As I promised before, here is the entire code for the "QueryProcessor" class, after including the additional methods that you saw before:
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; } }
Now that you know how the whole "QueryProcessor" class looks, 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(); }
Final thoughts
In this first tutorial of the series, you learned how to use some useful PHP networking functions, which were encapsulated within a PHP 5 class, in order to perform some common networking tasks, such as converting host names to the corresponding IP addresses and vice versa, listing Internet service names and TCP port numbers.
Nevertheless, I'm just scratching the surface of the PHP networking library. That's why in the next article I'll continue adding more methods to the "QueryProcessor" class, which will surely be quite interesting to you. Meet you in the next article!