1
|
|
|
<?php namespace Arcanedev\GeoIP\Drivers; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoIP\Tasks\DownloadContinentsFileTask; |
4
|
|
|
use Exception; |
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class IpApiDriver |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\GeoIP\Drivers |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class IpApiDriver extends AbstractDriver |
15
|
|
|
{ |
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
17
|
|
|
| Properties |
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
19
|
|
|
*/ |
20
|
|
|
/** |
21
|
|
|
* Http client instance. |
22
|
|
|
* |
23
|
|
|
* @var \GuzzleHttp\Client |
24
|
|
|
*/ |
25
|
|
|
protected $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* An array of continents. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $continents = []; |
33
|
|
|
|
34
|
|
|
/* ------------------------------------------------------------------------------------------------ |
35
|
|
|
| Main Functions |
36
|
|
|
| ------------------------------------------------------------------------------------------------ |
37
|
|
|
*/ |
38
|
|
|
/** |
39
|
|
|
* Init the driver. |
40
|
|
|
*/ |
41
|
18 |
|
protected function init() |
42
|
|
|
{ |
43
|
18 |
|
$this->client = new Client( |
44
|
18 |
|
$this->getHttpClientConfig() |
45
|
9 |
|
); |
46
|
|
|
|
47
|
|
|
// Set continents |
48
|
18 |
|
if (file_exists($path = $this->getOption('continents-path'))) { |
49
|
|
|
$this->continents = json_decode(file_get_contents($path), true); |
|
|
|
|
50
|
|
|
} |
51
|
18 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Locate the ip address. |
55
|
|
|
* |
56
|
|
|
* @param string $ipAddress |
57
|
|
|
* |
58
|
|
|
* @return \Arcanedev\GeoIP\Location |
59
|
|
|
* |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
6 |
|
public function locate($ipAddress) |
63
|
|
|
{ |
64
|
6 |
|
$response = $this->client->get("json/$ipAddress"); |
65
|
|
|
|
66
|
|
|
// Parse body content |
67
|
6 |
|
$data = json_decode($response->getBody()); |
68
|
|
|
|
69
|
|
|
// Verify response status |
70
|
6 |
|
if ($data->status !== 'success') { |
71
|
|
|
throw new Exception("Request failed ({$data->message})"); |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
return $this->hydrate([ |
75
|
6 |
|
'ip' => $ipAddress, |
76
|
6 |
|
'iso_code' => $data->countryCode, |
77
|
6 |
|
'country' => $data->country, |
78
|
6 |
|
'city' => $data->city, |
79
|
6 |
|
'state' => $data->regionName, |
80
|
6 |
|
'state_code' => $data->region, |
81
|
6 |
|
'postal_code' => $data->zip, |
82
|
6 |
|
'latitude' => $data->lat, |
83
|
6 |
|
'longitude' => $data->lon, |
84
|
6 |
|
'timezone' => $data->timezone, |
85
|
6 |
|
'continent' => $this->getContinent($data->countryCode), |
86
|
3 |
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Update function for service. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
* |
94
|
|
|
* @throws \Exception |
95
|
|
|
*/ |
96
|
|
|
public function update() |
97
|
|
|
{ |
98
|
|
|
return DownloadContinentsFileTask::run( |
99
|
|
|
'http://dev.maxmind.com/static/csv/codes/country_continent.csv', |
100
|
|
|
$this->getOption('continents-path') |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get continent based on country code. |
106
|
|
|
* |
107
|
|
|
* @param string $code |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
6 |
|
private function getContinent($code) |
112
|
|
|
{ |
113
|
6 |
|
return Arr::get($this->continents, $code, 'Unknown'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the http client config. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
18 |
|
private function getHttpClientConfig() |
122
|
|
|
{ |
123
|
|
|
$config = [ |
124
|
18 |
|
'base_uri' => 'http://ip-api.com/', |
125
|
|
|
'headers' => [ |
126
|
9 |
|
'User-Agent' => 'Laravel-GeoIP', |
127
|
9 |
|
], |
128
|
|
|
'query' => [ |
129
|
9 |
|
'fields' => 16895, |
130
|
9 |
|
], |
131
|
9 |
|
]; |
132
|
|
|
|
133
|
|
|
// Using the Pro service |
134
|
18 |
|
if ($this->getOption('key')) { |
135
|
|
|
$config['base_uri'] = $this->isSecure() ? 'https://pro.ip-api.com/' : 'http://pro.ip-api.com/'; |
136
|
|
|
$config['query']['key'] = $this->getOption('key'); |
137
|
|
|
} |
138
|
|
|
|
139
|
18 |
|
return $config; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check if secure url. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
private function isSecure() |
148
|
|
|
{ |
149
|
|
|
return (bool) $this->getOption('secure'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..