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\ServerErrorException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* Class TencentIp |
14
|
|
|
* |
15
|
|
|
* @package DucCnzj\Ip\Strategies |
16
|
|
|
*/ |
17
|
|
|
class TencentIp implements IpImp |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $url = 'https://apis.map.qq.com/ws/location/v1/ip'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $key; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* TencentIp constructor. |
31
|
|
|
* |
32
|
|
|
* @param $config |
33
|
|
|
*/ |
34
|
9 |
|
public function __construct($config = []) |
35
|
|
|
{ |
36
|
9 |
|
$this->setConfig($config); |
37
|
9 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array|string $config |
41
|
|
|
* |
42
|
|
|
* @return $this |
43
|
|
|
* |
44
|
|
|
* @author duc <[email protected]> |
45
|
|
|
*/ |
46
|
9 |
|
public function setConfig($config) |
47
|
|
|
{ |
48
|
9 |
|
if (is_array($config)) { |
49
|
9 |
|
$this->key = isset($config['key']) ? $config['key'] : ''; |
50
|
|
|
} else { |
51
|
2 |
|
$this->key = $config; |
52
|
|
|
} |
53
|
|
|
|
54
|
9 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ClientInterface $httpClient |
59
|
|
|
* @param string $ip |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
* @throws ServerErrorException |
63
|
|
|
* |
64
|
|
|
* @author duc <[email protected]> |
65
|
|
|
*/ |
66
|
4 |
|
public function send(ClientInterface $httpClient, string $ip): array |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
4 |
|
$originalStr = $httpClient->request('get', $this->url . '?ip=' . $ip . '&key=' . $this->getKey()) |
70
|
2 |
|
->getBody(); |
71
|
|
|
|
72
|
2 |
|
$result = json_decode($originalStr, true); |
73
|
|
|
|
74
|
2 |
|
if ($result['status'] !== 0) { |
75
|
1 |
|
throw new ServerErrorException($result['message']); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$data['ip'] = $ip; |
|
|
|
|
79
|
|
|
|
80
|
1 |
|
$data = $this->formatResult($data, $result); |
81
|
|
|
|
82
|
1 |
|
return $data; |
83
|
3 |
|
} catch (ServerException $e) { |
84
|
1 |
|
throw new ServerErrorException(); |
85
|
2 |
|
} catch (ClientException $e) { |
86
|
1 |
|
throw new ServerErrorException(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
* |
93
|
|
|
* @author duc <[email protected]> |
94
|
|
|
*/ |
95
|
5 |
|
public function getKey() |
96
|
|
|
{ |
97
|
5 |
|
return $this->key; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $result |
102
|
|
|
* @param array $data |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
* |
106
|
|
|
* @author duc <[email protected]> |
107
|
|
|
*/ |
108
|
1 |
|
public function formatResult(array $data, array $result) |
109
|
|
|
{ |
110
|
1 |
|
$data['country'] = $result['result']['ad_info']['nation']; |
111
|
1 |
|
$data['region'] = $result['result']['ad_info']['province']; |
112
|
1 |
|
$data['city'] = $result['result']['ad_info']['city']; |
113
|
1 |
|
$data['address'] = $data['country'] . $data['region'] . $data['city']; |
114
|
1 |
|
$data['point_x'] = $result['result']['location']['lng']; |
115
|
1 |
|
$data['point_y'] = $result['result']['location']['lat']; |
116
|
1 |
|
$data['isp'] = ''; |
117
|
|
|
|
118
|
1 |
|
return $data; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|