Home  >  Article  >  Backend Development  >  How to get ping time in php

How to get ping time in php

黄舟
黄舟Original
2017-09-30 09:10:151713browse

This article mainly introduces relevant information on how to obtain ping time in PHP. I hope this article can help everyone to achieve such a function. Friends in need can refer to it

PHP can execute shell commands through the exec function to obtain the ping time.

Code example:


echo &#39;PHP_INT_MAX : &#39; . PHP_INT_MAX . "<br><br>";
 
$ip = &#39;115.29.237.28&#39;;    // IP地址
 
if (PATH_SEPARATOR==&#39;:&#39;)  // linux
{
  echo &#39;I am linux&#39; . "<br><br>";
   
  exec("ping -c 3 -w 5 $ip", $info);
  echo &#39;linux info : &#39; . "<br>";
  print_r($info);
   
  $ping_time_line = end($info);
  echo &#39;<br><br>ping_time_line : &#39; . $ping_time_line . "<br><br>";
   
  $ping_time = explode("=", $ping_time_line)[1];
  $ping_time_min = explode("/", $ping_time)[0] / 1000.0;
  $ping_time_avg = explode("/", $ping_time)[1] / 1000.0;
  $ping_time_max = explode("/", $ping_time)[2] / 1000.0;
   
  echo $ping_time_min . " " . $ping_time_avg . " " . $ping_time_max . "<br><br>";
   
}
else             // windows
{
  echo &#39;I am windows&#39; . "<br><br>";
 
  exec("ping -c 3 -w 5 $ip", $info);
  echo &#39;windows info : &#39; . "<br>";
  print_r($info);
   
  $info_time = end($info);
  echo &#39;<br><br>info_time : &#39; . $info_time . "<br><br>";
}

Run result:


PHP_INT_MAX : 9223372036854775807

I am linux

linux info :

Array ( [0] => PING 115.29.237.28 (115.29.237.28) 56(84) bytes of data. [1] => 64 bytes from 115.29.237.28: icmp_seq=1 ttl=52 time=26.1 ms [2] => 64 bytes from 115.29.237.28: icmp_seq=2 ttl=52 time=27.5 ms [3] => 64 bytes from 115.29. 237.28: icmp_seq=3 ttl=52 time=25.2 ms [4] => [5] => — 115.29.237.28 ping statistics — [6] => 3 packets transmitted, 3 received, 0% packet loss, time 2002ms [7] => rtt min/avg/max/mdev = 25.280/26.339/27.590/0.970 ms )

ping_time_line : rtt min/avg/max/mdev = 25.280/26.339/27.590/0.970 ms

0.02528 0.026339 0.02759

Obtain the Ping time and encapsulate it into a function


##

function ping_time($ip) {
  $ping_cmd = "ping -c 3 -w 5 " . $ip;
  exec($ping_cmd, $info);
  $ping_time_line = end($info);
   
  $ping_time = explode("=", $ping_time_line)[1];
  $ping_time_min = explode("/", $ping_time)[0] / 1000.0;
  $ping_time_avg = explode("/", $ping_time)[1] / 1000.0;
  $ping_time_max = explode("/", $ping_time)[2] / 1000.0;
   
  $result = array();
  $result[&#39;ping_min&#39;] = $ping_time_min;
  $result[&#39;ping_avg&#39;] = $ping_time_avg;
  $result[&#39;ping_max&#39;] = $ping_time_max;
   
  print_r($result);
}
 
ping_time(&#39;115.29.237.28&#39;);

Running result:


Array ( [ping_min] => 0.025497 [ping_avg] => 0.025947 [ping_max] => 0.026753 )

The above is the detailed content of How to get ping time in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn