Ping a server using PHP

PEAR’s Net_Ping is a niffty wrapper class for executing ping calls from PHP. You can use it to check if a remote server is responding correctly. The library can be download fromĀ  here.


Net_Ping being a Pear package we will use the Pear installer to download and install it. I recommend to always use the Pear installer to download packages rather than downloading it manually as the Pear installer automatically downloads any dependent packages.

pear install Net_Ping-2.4.4


A example of using Net_Ping is given below.

    require_once "Net/Ping.php";
    $ping = Net_Ping::factory();
      echo $ping->getMessage();
      /* Number of packets to send */
      $ping->setArgs(array('count' => 4));
      $rawData = $ping->ping('');

The output of the same is shown below.

Net_Ping_Result Object
    [_icmp_sequence] => Array
            [1] => 310
            [2] => 300
            [3] => 318
            [4] => 301
    [_target_ip] =>
    [_bytes_per_request] => 32
    [_bytes_total] => 128
    [_ttl] => 238
    [_raw_data] => Array
            [0] => 
            [1] => Pinging [] with 32 bytes of data:
            [2] => 
            [3] => Reply from bytes=32 time=310ms TTL=238
            [4] => Reply from bytes=32 time=300ms TTL=238
            [5] => Reply from bytes=32 time=318ms TTL=238
            [6] => Reply from bytes=32 time=301ms TTL=238
            [7] => 
            [8] => Ping statistics for
            [9] =>     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
            [10] => Approximate round trip times in milli-seconds:
            [11] =>     Minimum = 300ms, Maximum = 318ms, Average = 307ms
    [_sysname] => windows
    [_round_trip] => Array
            [min] => 300
            [max] => 318
            [avg] => 307
    [_transmitted] => 4
    [_received] => 4
    [_loss] => 0

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at] for any new project requirements and price quotes.

5 Responses



September 13th, 2008 at 1:26 pm


I have found your post about pinging in php with the pear net_ping class.

Could you please tell me how I can use the output data? I have no clue how to use the data so I echo for example the IP adres on a page.

By thanking you in advance,


September 13th, 2008 at 8:35 pm

To get the IP element for example use the following:

$ip = $rawData->_target_ip;

and so on.

Now if you want tot get the ‘_raw_data’ element, you will see that it is an array, so we will have to loop through the same, like this:

foreach($rawData->_raw_data as $data)
echo $data . “\n”;



November 1st, 2008 at 6:45 am

can any one help me out how to print down the value of the array..?


November 1st, 2008 at 7:03 am

A easy way is to run the code from the command line and redirect the output to a text file, which you can print as shown below. Assuming your code filename is Ping.php.

c:\php Ping.php > pingdata.txt


Server Rack

May 7th, 2010 at 8:28 am

this is great stuff, thank you. i had to mess with this a little more than I wanted but finally got it.
Server Racks

Your thoughts

Sign up for fresh content in your email