1 | <?php |
||
14 | class IpInfo |
||
15 | { |
||
16 | /** |
||
17 | * @var |
||
18 | */ |
||
19 | private $ip; |
||
20 | /** |
||
21 | * @var |
||
22 | */ |
||
23 | private $info = [ |
||
24 | 'ip' => '', |
||
25 | 'country' => '', |
||
26 | 'area' => '', |
||
27 | 'region' => '', |
||
28 | 'city' => '', |
||
29 | 'county' => '', |
||
30 | 'isp' => '', |
||
31 | ]; |
||
32 | /** |
||
33 | * 请求淘宝 IP 库 api 超时时间 |
||
34 | * @var int |
||
35 | */ |
||
36 | private $outTime = 1; |
||
37 | |||
38 | /** |
||
39 | * IpInfo constructor. |
||
40 | * |
||
41 | * @param $ip |
||
42 | * |
||
43 | * @throws IpIllegalException |
||
44 | */ |
||
45 | 9 | public function __construct($ip) |
|
46 | { |
||
47 | 9 | $this->ip = $ip; |
|
48 | 9 | $this->query(); |
|
49 | 9 | } |
|
50 | |||
51 | /** |
||
52 | * @param string $delimiter |
||
53 | * @param bool $full |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | 9 | public function address($delimiter = ' ', $full = false) |
|
58 | { |
||
59 | $struct = [ |
||
60 | 9 | $this->info['country'], |
|
61 | 9 | $this->info['area'], |
|
62 | 9 | $this->info['region'], |
|
63 | 9 | $this->info['city'], |
|
64 | 9 | $this->info['county'], |
|
65 | 9 | ]; |
|
66 | 9 | if (!$full) { |
|
67 | 9 | array_splice($struct, 1, 1); |
|
68 | 9 | } |
|
69 | 9 | $struct = array_filter($struct); |
|
70 | |||
71 | 9 | return implode($delimiter, $struct); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return array ip 的信息 |
||
76 | */ |
||
77 | 1 | public function info() |
|
81 | |||
82 | /** |
||
83 | * @param $name |
||
84 | * @param $arguments |
||
85 | * |
||
86 | * @return mixed |
||
87 | * @throws MethodNotExistException |
||
88 | */ |
||
89 | 7 | public function __call($name, $arguments) |
|
98 | |||
99 | /** |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public function getOutTime() |
||
106 | |||
107 | /** |
||
108 | * @param mixed $outTime |
||
109 | * |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function setOutTime($outTime) |
||
118 | |||
119 | /** |
||
120 | * @throws GetIpIpInfoFailedException |
||
121 | */ |
||
122 | 9 | private function query() |
|
123 | { |
||
124 | 9 | $this->info['ip'] = $this->ip; |
|
125 | 9 | if ($this->ip === '127.0.0.1') { |
|
126 | $this->info['country'] = '本机'; |
||
127 | 9 | } elseif ($this->isInternal()) { |
|
128 | $this->info['country'] = 'INNA 保留地址'; |
||
129 | } else { |
||
130 | // 先通过 api 获取,api 获取失败的话通过本地数据库获取 |
||
131 | 9 | $result = $this->apiQuery(); |
|
132 | 9 | if ($result === false) { |
|
133 | // api 获取失败的话进行本地获取 |
||
134 | $result = $this->localQuery(); |
||
135 | } |
||
136 | |||
137 | 9 | $this->info = $result; |
|
138 | } |
||
139 | 9 | } |
|
140 | |||
141 | 9 | private function apiQuery() |
|
145 | |||
146 | private function localQuery() |
||
147 | { |
||
148 | return IpLocalQuery::create()->query($this->ip); |
||
149 | } |
||
150 | |||
151 | 9 | private function isInternal() |
|
161 | |||
162 | /** |
||
163 | * @param $value |
||
164 | * @param string $delimiter |
||
165 | * |
||
166 | * @return mixed|string |
||
167 | */ |
||
168 | 7 | private function snakeCase($value, $delimiter = '_') |
|
177 | } |