ping主机接口,获取参数 url ,返回延迟时间及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();
}
?>