网站Ping接口源码,接口利用的百度开发接口制作,接口可查IP地址和网站测速。
API接口代码
<?php $hh=$_GET['hh']?:"\n";//换行符号(默认\n) if($_GET['url']==null){ echo "请输入参数!"; exit(); } if (preg_match('/http:\/\//i', $_GET['url'])|preg_match('/https:\/\//i', $_GET['url'])) { echo '不能带http://或https://,只需要输入域名!'; exit(); } $msg = $_GET['url']; $host = $msg; $port = '80'; $num = 2; //Ping次数 $tip = gethostbyname($_GET['url']); //获取时间 function mt_f (){ list($usec,$sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); //微秒加秒 } function ping_f($host,$port){ $time_s = mt_f(); $ip = gethostbyname($host); $fp = @fsockopen($host,$port); if(!$fp) return '测试超时'; $get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnect:".$port."Close\r\n"; fputs($fp,$get); fclose($fp); $time_e = mt_f(); $time = $time_e - $time_s; $time = ceil($time * 1000); return $time; } if(ping_f($host,$port)=='测试超时'){ echo '测试超时,请检查目标站点是否正常!'; }else{ for($i = 0;$i < $num;$i++){ if($i==0){ $s1=$t.ping_f($host,$port); }elseif($i == 1){ $s2=$t.ping_f($host,$port); } //每次运行中间间隔1S sleep(1); //刷新输出缓存 ob_flush(); flush(); } $se=json_decode(file_get_contents("http://opendata.baidu.com/api.php?query=".$tip."&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=utf-8&format=json"),true); $dz=$se["data"][0]["location"]; if($s1>$s2){ echo '查询数据:'.$msg.''.$hh.''; echo 'IP地址:'.$tip.''.$hh.''; echo '主机地址:'.$dz.''.$hh.''; echo '最慢:'.$s1.'/ms'.$hh.''; echo '最快:'.$s2.'/ms'.$hh.''; echo '平均:'.(($s1-$s2)/2+$s2).'/ms'; }else{ echo '查询数据:'.$msg.''.$hh.''; echo 'IP地址:'.$tip.''.$hh.''; echo '主机地址:'.$dz.''.$hh.''; echo '最慢:'.$s1.'/ms'.$hh.''; echo '最快:'.$s2.'/ms'.$hh.''; echo '平均:'.(($s1-$s2)/2+$s2).'/ms'; } } ?>
返回格式示例
{"status":"0","t":"1433920989928","set_cache_time":"","data":[{"ExtendedLocation":"","OriginQuery":"114.158.169.85","appinfo":"","disp_type":0,"fetchkey":"114.158.169.85","location":"日本神奈川","origip":"114.158.169.85","origipquery":"114.158.169.85","resourceid":"6006","role_id":0,"shareImage":1,"showLikeShare":1,"showlamp":"1","titlecont":"IP地址查询","tplt":"ip"}]}
美化下
{ "status": "0", "t": "1433920989928", "set_cache_time": "", "data": [{ "ExtendedLocation": "", "OriginQuery": "114.158.169.85", "appinfo": "", "disp_type": 0, "fetchkey": "114.158.169.85", "location": "日本神奈川", "origip": "114.158.169.85", "origipquery": "114.158.169.85", "resourceid": "6006", "role_id": 0, "shareImage": 1, "showLikeShare": 1, "showlamp": "1", "titlecont": "IP地址查询", "tplt": "ip" }] }