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
|
|
|
class TaobaoIp implements IpImp |
12
|
|
|
{ |
13
|
|
|
protected $url = 'http://ip.taobao.com/service/getIpInfo.php'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param ClientInterface $httpClient |
17
|
|
|
* |
18
|
|
|
* @param string $ip |
19
|
|
|
* |
20
|
|
|
* @return array |
21
|
|
|
* @throws ServerErrorException |
22
|
|
|
* @author duc <[email protected]> |
23
|
|
|
*/ |
24
|
5 |
|
public function send(ClientInterface $httpClient, string $ip):array |
25
|
|
|
{ |
26
|
|
|
try { |
27
|
5 |
|
$originalStr = $httpClient->request('get', $this->url . '?ip=' . $ip) |
28
|
3 |
|
->getBody(); |
29
|
|
|
|
30
|
3 |
|
$result = json_decode($originalStr, true); |
31
|
|
|
|
32
|
3 |
|
if ($result['code'] !== 0) { |
33
|
1 |
|
throw new ServerErrorException(); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
$data['ip'] = $ip; |
|
|
|
|
37
|
|
|
|
38
|
2 |
|
$data = $this->formatResult($data, $result); |
39
|
|
|
|
40
|
2 |
|
return $data; |
41
|
3 |
|
} catch (ServerException $e) { |
42
|
1 |
|
throw new ServerErrorException(); |
43
|
2 |
|
} catch (ClientException $e) { |
44
|
1 |
|
throw new ServerErrorException(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $result |
50
|
|
|
* @param array $data |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
* |
54
|
|
|
* @author duc <[email protected]> |
55
|
|
|
*/ |
56
|
2 |
|
public function formatResult(array $data, array $result) |
57
|
|
|
{ |
58
|
2 |
|
$data['country'] = $result['data']['country']; |
59
|
2 |
|
$data['region'] = $result['data']['region']; |
60
|
2 |
|
$data['city'] = $result['data']['city']; |
61
|
2 |
|
$data['address'] = $data['country'] . $data['region'] . $data['city']; |
62
|
2 |
|
$data['point_x'] = ''; |
63
|
2 |
|
$data['point_y'] = ''; |
64
|
2 |
|
$data['isp'] = $result['data']['isp']; |
65
|
|
|
|
66
|
2 |
|
return $data; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|