本地ping域名API的源码

pingAPI请求方式

Method: GET

请求地址

https://www.x.com/源码名称.php?url=

参数

url=要ping的域名

调用示例

https://www.x.com/pingapi.php?url=www.baidu.com

源码

<?php
$url = $_GET['url'];
$port = '80';
$num = 1; //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();
}
?>