Ping A Website Using PEAR's Net_Ping

22nd April 2009 - 5 minutes read time

Net_Ping is a PHP wrapper for the ping program and provides a neat way pinging a host or checking if a site is up and running. To install it you just need to have pear installed. Once that is done you can do a search for PEAR Net_Ping by opening a terminal and typing:

> ping search ping

This will return the following:

  1. Retrieving data...0%.MATCHED PACKAGES, CHANNEL PEAR.PHP.NET:
  2. =======================================
  3. PACKAGE STABLE/(LATEST) LOCAL
  4. Net_Ping 2.4.4 (stable) Execute ping
  5. Services_Pingback 0.2.2 (alpha) A Pingback User-Agent class.

You can install Net_Ping by running the following:

> pear install Net_Ping

Which should produce the following output.

  1. downloading Net_Ping-2.4.4.tgz ...
  2. Starting to download Net_Ping-2.4.4.tgz (9,563 bytes)
  3. .....done: 9,563 bytes
  4. install ok: channel://pear.php.net/Net_Ping-2.4.4

You can now use Net_Ping. To use the class in your code you need to include it first, after this you need to get an instance of the Net_Ping object so that you can work with it. The following code will create a Net_Ping object that you can work with.

  1. require_once "Net/Ping.php";
  2. $ping = Net_Ping::factory();

To ping a site you just need to run the ping() function. This function takes a single parameter which is the IP address or the name of the server you want to ping. The function returns a Net_Ping_Result object that contains all of the information returned from the ping.

$results = $ping->ping('www.example.com');

Here is a print_r() output of this object.

  1. Net_Ping_Result Object
  2. (
  3. [_icmp_sequence] => Array
  4. (
  5. [1] => 176
  6. [2] => 177
  7. )
  8.  
  9. [_target_ip] => 208.77.188.166
  10. [_bytes_per_request] => 32
  11. [_bytes_total] => 64
  12. [_ttl] => 54
  13. [_raw_data] => Array
  14. (
  15. [0] =>
  16. [1] => Pinging www.example.com [208.77.188.166] with 32 bytes of data:
  17. [2] =>
  18. [3] => Reply from 208.77.188.166: bytes=32 time=176ms TTL=54
  19. [4] => Reply from 208.77.188.166: bytes=32 time=177ms TTL=54
  20. [5] =>
  21. [6] => Ping statistics for 208.77.188.166:
  22. [7] => Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
  23. [8] => Approximate round trip times in milli-seconds:
  24. [9] => Minimum = 176ms, Maximum = 177ms, Average = 176ms
  25. )
  26.  
  27. [_sysname] => windows
  28. [_round_trip] => Array
  29. (
  30. [min] => 176
  31. [max] => 177
  32. [avg] => 176
  33. )
  34.  
  35. [_transmitted] => 2
  36. [_received] => 2
  37. [_loss] => 0
  38. )

You can access the different properties within the Net_Ping_Result to find out what happened with the ping. The percentage of pings lost is also worked out for you in the _loss property.

  1. echo '<p>Transmitted: '.$results->_transmitted.'</p>';
  2. echo '<p>Received: '.$results->_received.'</p>';
  3. echo '<p>% Loss: '.$results->_loss.'%</p>';

The _round_trip parameter contains an array that details the minimum, maximum and average times for the requests.

You might have noticed in my original example that I only sent out 2 pings. This is because the Net_Ping object has a function that allows you to set certain options before you make the request. This function is called setArgs() and takes an associative array as the argument. To set the number of ping requests to be sent out just use the following line of code before you make your ping request.

$ping->setArgs(array('count' => 2));

You should be aware that different operating systems have different options, so for a full list of options please see the Net_Ping::setArgs() page on the PEAR website.

If you just want to check the host to make sure it is still active you can use the checkhost() function. This works in the same way as the ping() function, but will return true on success and false on failure. You can see the output of checkhost() by viewing the var_dump() output.

var_dump($ping->checkhost('example.com'));

This function is really a wrapper for the ping() function, but will parse the output to determine if the ping request worked or not.

Add new comment

The content of this field is kept private and will not be shown publicly.