php如何擷取ping時間的方法

來源:互聯網
上載者:User
這篇文章主要介紹了PHP 擷取 ping 時間的實現方法的相關資料,希望通過本文能協助到大家,讓大家實現這樣的功能,需要的朋友可以參考下

PHP 可以通過exec函數執行shell命令,來擷取ping時間。

程式碼範例:


echo 'PHP_INT_MAX : ' . PHP_INT_MAX . "<br><br>"; $ip = '115.29.237.28';    // IP地址 if (PATH_SEPARATOR==':')  // linux{  echo 'I am linux' . "<br><br>";     exec("ping -c 3 -w 5 $ip", $info);  echo 'linux info : ' . "<br>";  print_r($info);     $ping_time_line = end($info);  echo '<br><br>ping_time_line : ' . $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 'I am windows' . "<br><br>";   exec("ping -c 3 -w 5 $ip", $info);  echo 'windows info : ' . "<br>";  print_r($info);     $info_time = end($info);  echo '<br><br>info_time : ' . $info_time . "<br><br>";}

運行結果:


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

擷取Ping時間封裝成函數


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['ping_min'] = $ping_time_min;  $result['ping_avg'] = $ping_time_avg;  $result['ping_max'] = $ping_time_max;     print_r($result);} ping_time('115.29.237.28');

運行結果:


Array ( [ping_min] => 0.025497 [ping_avg] => 0.025947 [ping_max] => 0.026753 )
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.