|
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\ServerErrorException; |
|
11
|
|
|
use DucCnzj\Ip\Exceptions\UnauthorizedException; |
|
12
|
|
|
|
|
13
|
|
|
class AliIp implements IpImp |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $appCode; |
|
19
|
|
|
|
|
20
|
|
|
protected $url = 'http://iploc.market.alicloudapi.com/v3/ip'; |
|
21
|
|
|
|
|
22
|
10 |
|
public function __construct($config = []) |
|
23
|
|
|
{ |
|
24
|
10 |
|
$this->setConfig($config); |
|
25
|
10 |
|
} |
|
26
|
|
|
|
|
27
|
10 |
|
public function setConfig($config = []): IpImp |
|
28
|
|
|
{ |
|
29
|
10 |
|
$this->appCode = isset($config['app_code']) ? $config['app_code'] : ''; |
|
30
|
|
|
|
|
31
|
10 |
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return string |
|
36
|
|
|
* |
|
37
|
|
|
* @author duc <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
6 |
|
public function getAppCode() |
|
40
|
|
|
{ |
|
41
|
6 |
|
return $this->appCode; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
5 |
|
public function send(ClientInterface $client, string $ip): array |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
5 |
|
$originalStr = $client->request( |
|
48
|
5 |
|
'get', |
|
49
|
5 |
|
$this->url . '?ip=' . $ip, |
|
50
|
|
|
[ |
|
51
|
|
|
'headers' => [ |
|
52
|
5 |
|
'Authorization' => "APPCODE {$this->getAppCode()}", |
|
53
|
|
|
], |
|
54
|
|
|
] |
|
55
|
2 |
|
)->getBody(); |
|
56
|
|
|
|
|
57
|
2 |
|
$result = json_decode($originalStr, true); |
|
58
|
|
|
|
|
59
|
2 |
|
if ($result['rectangle'] === []) { |
|
60
|
1 |
|
throw new AnalysisException('ip 地址解析失败'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
$data = ['ip' => $ip]; |
|
64
|
1 |
|
$data = $this->formatResult($data, $result); |
|
65
|
|
|
|
|
66
|
1 |
|
return $data; |
|
67
|
4 |
|
} catch (ServerException $e) { |
|
68
|
1 |
|
throw new ServerErrorException($e->getMessage()); |
|
69
|
3 |
|
} catch (ClientException $e) { |
|
70
|
2 |
|
if ($e->getResponse()->getStatusCode() === 401) { |
|
71
|
1 |
|
throw new UnauthorizedException($e->getMessage()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
throw new ServerErrorException($e->getMessage()); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
public function formatResult($data, $result) |
|
79
|
|
|
{ |
|
80
|
|
|
// 116.0119343,39.66127144;116.7829835,40.2164962 |
|
81
|
1 |
|
$arr = explode(';', $result['rectangle']); |
|
82
|
1 |
|
$rectangle = explode(',', $arr[0]); |
|
83
|
1 |
|
$pointX = $rectangle[0]; |
|
84
|
1 |
|
$pointY = $rectangle[1]; |
|
85
|
1 |
|
$data['city'] = $result['city']; |
|
86
|
1 |
|
$data['region'] = $result['province']; |
|
87
|
1 |
|
$data['country'] = '中国'; |
|
88
|
1 |
|
$data['point_x'] = $pointX; |
|
89
|
1 |
|
$data['point_y'] = $pointY; |
|
90
|
1 |
|
$data['isp'] = ''; |
|
91
|
1 |
|
$data['address'] = $data['country'] . $data['region'] . $data['city']; |
|
92
|
|
|
|
|
93
|
1 |
|
return $data; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|