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