PHP
  Home arrow PHP arrow Page 4 - Build a Query Processor Class for Networking in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Build a Query Processor Class for Networking in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-05-17


    Table of Contents:
  • Build a Query Processor Class for Networking in PHP 5
  • Building the class: Converting host names to IP addresses and vice versa
  • Handling Internet services: defining the "getServiceNames()" and "getServicePorts()" methods
  • Assembling the methods: listing the full source code of the "QueryProcessor" class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Build a Query Processor Class for Networking in PHP 5 - Assembling the methods: listing the full source code of the "QueryProcessor" class
    ( Page 4 of 4 )

    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! 



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek