forked from helloxz/IPinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
75 lines (70 loc) · 1.44 KB
/
api.php
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
69
70
71
72
73
74
75
<?php
header('Access-Control-Allow-Origin:*');
error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
include_once('./config.php');
//载入纯真IP
include_once( 'class/qqwry.php' );
include_once("class/Query.Class.php");
$ip = $_GET['ip']; //获取IP
$type = $_GET['type']; //获取数据类型
$data = $_GET['data']; //要返回的数据
//如果IP不存在或者为空
if((!isset($ip) || ($ip == ''))) {
//获取访客IP
$ip = $_SERVER["REMOTE_ADDR"];
}
//如果IP格式不对
if(!filter_var($ip, FILTER_VALIDATE_IP)) {
$reinfo = array(
"status" => 0,
"msg" => 'IP格式不对'
);
echo json_encode($reinfo);
exit;
}
//查询IP
//$qqwry = new ip();
$addr = $query->caches($ip,'qqwry');
$addr = json_decode($addr);
$addr = $addr->address;
if($addr == null){
$addr = $query->caches($ip,'qqwry');
$addr = json_decode($addr);
$addr = $addr->address;
}
//$addr = $qqwry -> ip2addr("$ip");
//$addr = array_slice($addr,2,4);
//$addr = implode(" ",$addr);
//返回不同的数据
switch ( $data )
{
case 'ip':
$addr = '';
break;
case 'addr':
$ip = '';
break;
default:
$ip = $ip;
$addr = $addr;
break;
}
//根据类型返回不同结果
switch ( $type )
{
case 'text':
echo $ip." ".$addr;
break;
case 'json':
$info = array(
"status" => 1,
"ip" => $ip,
"addr" => $addr
);
echo json_encode($info);
exit;
default:
echo $ip." ".$addr."\n";
break;
}
?>