Ping a server using PHP


Posted in: php | Save to del.icio.us | Twit This! 13 Sep 2008

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.

Installation

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

Usage

A example of using Net_Ping is given below.

<?php
 
    require_once "Net/Ping.php";
 
    $ping = Net_Ping::factory();
 
    if(PEAR::isError($ping))
      echo $ping->getMessage();
    else
    {
      /* Number of packets to send */
      $ping->setArgs(array('count' => 4));
      $rawData = $ping->ping('google.com');
      print_r($rawData);
    }
 
?>

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] => 64.233.167.99
    [_bytes_per_request] => 32
    [_bytes_total] => 128
    [_ttl] => 238
    [_raw_data] => Array
        (
            [0] => 
            [1] => Pinging google.com [64.233.167.99] with 32 bytes of data:
            [2] => 
            [3] => Reply from 64.233.167.99: bytes=32 time=310ms TTL=238
            [4] => Reply from 64.233.167.99: bytes=32 time=300ms TTL=238
            [5] => Reply from 64.233.167.99: bytes=32 time=318ms TTL=238
            [6] => Reply from 64.233.167.99: bytes=32 time=301ms TTL=238
            [7] => 
            [8] => Ping statistics for 64.233.167.99:
            [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
)



Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts



4 Responses

1

BKS

September 13th, 2008 at 1:26 pm

Hello,

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,

sameer

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”;

3

Hasan

November 1st, 2008 at 6:45 am

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

sameer

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

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]
  • Martin: How can you carry over your own variables into the URL? I am using a form to POST a couple of var [...]
  • nancy: thanks very much ! first tools [...]
  • Krishna | PHP Programmer: Thanks for sharing !! [...]
  • Really Useful Tutorials You Should Have Read in February 2010 Ajax Help W3C Tag: [...] Reading Google Analytics data from PHP By Sameer Borate, February 1st, 2010 Site: Code Diesel [...]

  • Users Online

    • 14 Users Online
    • 12 Guests, 2 Bots