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 | 8 | private $outTime = 1; |
|
37 | |||
38 | 8 | /** |
|
39 | 8 | * IpInfo constructor. |
|
40 | * |
||
41 | * @param $ip |
||
42 | 8 | * |
|
43 | 8 | * @throws IpIllegalException |
|
44 | */ |
||
45 | public function __construct($ip) |
||
46 | { |
||
47 | $this->ip = $ip; |
||
48 | $this->query(); |
||
49 | } |
||
50 | |||
51 | 8 | /** |
|
52 | * @param string $delimiter |
||
53 | 8 | * @param bool $full |
|
54 | * |
||
55 | * @return string |
||
56 | 8 | */ |
|
57 | public function address($delimiter = ' ', $full = false) |
||
58 | { |
||
59 | $struct = [ |
||
60 | 8 | $this->info['country'], |
|
61 | 8 | $this->info['area'], |
|
62 | 8 | $this->info['region'], |
|
63 | 8 | $this->info['city'], |
|
64 | 8 | $this->info['county'], |
|
65 | 8 | ]; |
|
66 | 8 | if (!$full) { |
|
67 | 8 | array_splice($struct, 1, 1); |
|
68 | 8 | } |
|
69 | 8 | $struct = array_filter($struct); |
|
70 | |||
71 | 8 | 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 | 8 | public function getOutTime() |
|
106 | 8 | ||
107 | /** |
||
108 | * @param mixed $outTime |
||
109 | 8 | * |
|
110 | * @return $this |
||
111 | 8 | */ |
|
112 | public function setOutTime($outTime) |
||
118 | 8 | ||
119 | /** |
||
120 | * @throws GetIpIpInfoFailedException |
||
121 | 8 | */ |
|
122 | private function query() |
||
140 | |||
141 | private function apiQuery() |
||
145 | |||
146 | private function localQuery() |
||
150 | |||
151 | private function isInternal() |
||
161 | |||
162 | /** |
||
163 | * @param $value |
||
164 | * @param string $delimiter |
||
165 | * |
||
166 | * @return mixed|string |
||
167 | */ |
||
168 | private function snakeCase($value, $delimiter = '_') |
||
177 | } |