1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DucCnzj\Ip\Strategies; |
4
|
|
|
|
5
|
|
|
use DucCnzj\Ip\Imp\IpImp; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
8
|
|
|
use GuzzleHttp\Exception\ServerException; |
9
|
|
|
use DucCnzj\Ip\Exceptions\AnalysisException; |
10
|
|
|
use DucCnzj\Ip\Exceptions\BreakLoopException; |
11
|
|
|
use DucCnzj\Ip\Exceptions\ServerErrorException; |
12
|
|
|
|
13
|
|
|
class AliIp implements IpImp |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $appCode; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $url = 'http://iploc.market.alicloudapi.com/v3/ip'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AliIp constructor. |
27
|
|
|
* |
28
|
|
|
* @param array $config |
29
|
|
|
*/ |
30
|
5 |
|
public function __construct($config = []) |
31
|
|
|
{ |
32
|
5 |
|
$this->setConfig($config); |
33
|
5 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array|string $config |
37
|
|
|
* |
38
|
|
|
* @return IpImp |
39
|
|
|
* |
40
|
|
|
* @author duc <[email protected]> |
41
|
|
|
*/ |
42
|
5 |
|
public function setConfig($config): IpImp |
43
|
|
|
{ |
44
|
5 |
|
if (is_array($config)) { |
45
|
5 |
|
$this->appCode = isset($config['app_code']) ? $config['app_code'] : ''; |
46
|
|
|
} else { |
47
|
1 |
|
$this->appCode = $config; |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
* |
56
|
|
|
* @author duc <[email protected]> |
57
|
|
|
*/ |
58
|
5 |
|
public function getAppCode() |
59
|
|
|
{ |
60
|
5 |
|
return $this->appCode; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param ClientInterface $client |
65
|
|
|
* @param string $ip |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
* @throws AnalysisException |
69
|
|
|
* @throws BreakLoopException |
70
|
|
|
* @throws ServerErrorException |
71
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
72
|
|
|
* |
73
|
|
|
* @author duc <[email protected]> |
74
|
|
|
*/ |
75
|
4 |
|
public function send(ClientInterface $client, string $ip): array |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
4 |
|
$originalStr = $client->request( |
79
|
4 |
|
'get', |
80
|
4 |
|
$this->url . '?ip=' . $ip, |
81
|
|
|
[ |
82
|
|
|
'headers' => [ |
83
|
4 |
|
'Authorization' => "APPCODE {$this->getAppCode()}", |
84
|
|
|
], |
85
|
|
|
] |
86
|
2 |
|
)->getBody(); |
87
|
|
|
|
88
|
2 |
|
$result = json_decode($originalStr, true); |
89
|
|
|
|
90
|
2 |
|
if ($result['rectangle'] === []) { |
91
|
1 |
|
throw new AnalysisException('ip 地址解析失败'); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$data = ['ip' => $ip]; |
95
|
1 |
|
$data = $this->formatResult($data, $result); |
96
|
|
|
|
97
|
1 |
|
return $data; |
98
|
3 |
|
} catch (ServerException $e) { |
99
|
1 |
|
throw new ServerErrorException($e->getMessage()); |
100
|
2 |
|
} catch (ClientException $e) { |
101
|
1 |
|
throw new BreakLoopException($e->getMessage()); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $data |
107
|
|
|
* @param array $result |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
* |
111
|
|
|
* @author duc <[email protected]> |
112
|
|
|
*/ |
113
|
1 |
|
public function formatResult(array $data, array $result) |
114
|
|
|
{ |
115
|
|
|
// 116.0119343,39.66127144;116.7829835,40.2164962 |
116
|
1 |
|
$arr = explode(';', $result['rectangle']); |
117
|
1 |
|
$rectangle = explode(',', $arr[0]); |
118
|
1 |
|
$pointX = $rectangle[0]; |
119
|
1 |
|
$pointY = $rectangle[1]; |
120
|
1 |
|
$data['city'] = $result['city']; |
121
|
1 |
|
$data['region'] = $result['province']; |
122
|
1 |
|
$data['country'] = '中国'; |
123
|
1 |
|
$data['point_x'] = $pointX; |
124
|
1 |
|
$data['point_y'] = $pointY; |
125
|
1 |
|
$data['isp'] = ''; |
126
|
1 |
|
$data['address'] = $data['country'] . $data['region'] . $data['city']; |
127
|
|
|
|
128
|
1 |
|
return $data; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|