本地ping API

2021-01-14T14:43:00

ping主机接口,获取参数 [c=red]url[/c] ,返回延迟时间及ip,测试api站

代码实现

<?php
$url = $_GET['url'];
$port = '80';
$num = 4; //Ping次数
function mt_f (){
list($usec,$sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function ping_f($url,$port){
$time_s = mt_f();
$ip = gethostbyname($url);
$fp = @fsockopen($url,$port);
if(!$fp)
return '回复超时!';
$get = "GET / HTTP/1.1||Host:".$url."||Connect:".$port."Close||";
fputs($fp,$get);
fclose($fp);
$time_e = mt_f();
$time = $time_e - $time_s;
$time = ceil($time * 1000);
return '时间 = '.$time.'ms';
}
for($i = 0;$i < $num;$i++){
echo '正在ping:'.$url.' 来自 '.gethostbyname($url).' 的回复 '.ping_f($url,$port).'</br>';
sleep(0.1);//每次运行中间间隔0.1s
ob_flush();
flush();
}
?>

结语

感谢访问强仔博客,希望本文对你有所帮助!

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »