文章摘要
本文介绍了利用百度开发接口实现网站Ping测试的方法。文章提供了PHP代码,用于获取目标网站的IP地址和主机信息,并通过Ping接口进行测试。代码中定义了Ping次数、获取时间以及数据处理函数,并展示了如何通过百度开发接口获取测试结果。测试结果返回包含IP地址、主机位置、 ping数据等信息的JSON格式。用户只需输入URL和参数即可使用该工具进行Ping测试,方便快捷。
网站Ping接口源码,接口利用的百度开发接口制作,接口可查IP地址和网站测速。
API接口代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
<?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);}//每次运行中间间隔1Ssleep(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';}}?> |
返回格式示例
|
1
|
{"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"}]} |
美化下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
{ "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" }]} |
使用方法:
[yk_WeChatFans]http://winvvv.com/tool/ping.php?url=winvvv.com&hh=%3Cbr%3E&num=3[/yk_WeChatFans]
© 版权声明
文章版权归作者所有,未经允许请勿转载。