1
|
|
|
<?php namespace Arcanedev\GeoIP\Drivers; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoIP\Entities\Continents; |
4
|
|
|
use Exception; |
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class IpApiDriver |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\GeoIP\Drivers |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class IpApiDriver extends AbstractDriver |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Properties |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Http client instance. |
22
|
|
|
* |
23
|
|
|
* @var \GuzzleHttp\Client |
24
|
|
|
*/ |
25
|
|
|
protected $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A collection of continents. |
29
|
|
|
* |
30
|
|
|
* @var \Arcanedev\GeoIP\Entities\Continents |
31
|
|
|
*/ |
32
|
|
|
protected $continents; |
33
|
|
|
|
34
|
|
|
/* ----------------------------------------------------------------- |
35
|
|
|
| Main Methods |
36
|
|
|
| ----------------------------------------------------------------- |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Init the driver. |
41
|
|
|
*/ |
42
|
9 |
|
protected function init() |
43
|
|
|
{ |
44
|
9 |
|
$this->client = new Client( |
45
|
9 |
|
$this->getHttpClientConfig() |
46
|
|
|
); |
47
|
|
|
|
48
|
9 |
|
$this->continents = Continents::load(); |
49
|
9 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Locate the ip address. |
53
|
|
|
* |
54
|
|
|
* @param string $ipAddress |
55
|
|
|
* |
56
|
|
|
* @return \Arcanedev\GeoIP\Location |
57
|
|
|
* |
58
|
|
|
* @throws \Exception |
59
|
|
|
*/ |
60
|
3 |
|
public function locate($ipAddress) |
61
|
|
|
{ |
62
|
3 |
|
$response = $this->client->get("json/$ipAddress"); |
63
|
|
|
|
64
|
|
|
// Parse body content |
65
|
3 |
|
$data = json_decode($response->getBody()); |
66
|
|
|
|
67
|
|
|
// Verify response status |
68
|
3 |
|
if ($data->status !== 'success') { |
69
|
|
|
throw new Exception("Request failed ({$data->message})"); |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
return $this->hydrate([ |
73
|
3 |
|
'ip' => $ipAddress, |
74
|
3 |
|
'iso_code' => $data->countryCode, |
75
|
3 |
|
'country' => $data->country, |
76
|
3 |
|
'city' => $data->city, |
77
|
3 |
|
'state' => $data->regionName, |
78
|
3 |
|
'state_code' => $data->region, |
79
|
3 |
|
'postal_code' => $data->zip, |
80
|
3 |
|
'latitude' => $data->lat, |
81
|
3 |
|
'longitude' => $data->lon, |
82
|
3 |
|
'timezone' => $data->timezone, |
83
|
3 |
|
'continent' => $this->continents->get($data->countryCode, 'Unknown'), |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the http client config. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
9 |
|
private function getHttpClientConfig() |
93
|
|
|
{ |
94
|
|
|
$config = [ |
95
|
9 |
|
'base_uri' => 'http://ip-api.com/', |
96
|
|
|
'headers' => [ |
97
|
|
|
'User-Agent' => 'Laravel-GeoIP', |
98
|
|
|
], |
99
|
|
|
'query' => [ |
100
|
|
|
'fields' => 16895, |
101
|
|
|
], |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
// Using the Pro service |
105
|
9 |
|
if ($this->getOption('key')) { |
106
|
|
|
$config['base_uri'] = $this->isSecure() ? 'https://pro.ip-api.com/' : 'http://pro.ip-api.com/'; |
107
|
|
|
$config['query']['key'] = $this->getOption('key'); |
108
|
|
|
} |
109
|
|
|
|
110
|
9 |
|
return $config; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check if secure url. |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
private function isSecure() |
119
|
|
|
{ |
120
|
|
|
return (bool) $this->getOption('secure'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|