1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lefty\Weather; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
9
|
|
|
// use Anax\Route\Exception\NotFoundException; |
10
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class WeatherGeoLocation implements ContainerInjectableInterface |
19
|
|
|
{ |
20
|
|
|
use ContainerInjectableTrait; |
21
|
|
|
|
22
|
|
|
private $curl = ""; |
23
|
|
|
private $geoLocation = ""; |
24
|
|
|
private $apiKey = ""; |
25
|
|
|
|
26
|
|
|
|
27
|
6 |
|
public function setAPI(string $key) |
28
|
|
|
{ |
29
|
6 |
|
$this->apiKey = $this->di->get("keystore")->getKey($key); |
30
|
6 |
|
} |
31
|
|
|
|
32
|
6 |
|
public function checkGeoLocation($ipAddress) |
33
|
|
|
{ |
34
|
|
|
// $this->apiKey = $this->di->get("keystore")->getKey("ipstack"); |
35
|
6 |
|
$this->geoInitCurl(); |
36
|
6 |
|
$this->geoSetOptCurl($ipAddress); |
37
|
6 |
|
$this->geoExecuteCurl(); |
38
|
6 |
|
$this->geoCloseCurl(); |
39
|
6 |
|
} |
40
|
|
|
|
41
|
6 |
|
private function geoInitCurl() |
42
|
|
|
{ |
43
|
6 |
|
$this->curl = curl_init(); |
44
|
6 |
|
} |
45
|
|
|
|
46
|
6 |
|
private function geoSetOptCurl($ipAddress) |
47
|
|
|
{ |
48
|
6 |
|
curl_setopt($this->curl, CURLOPT_URL, "http://api.ipstack.com/" . $ipAddress . "?access_key=" . $this->apiKey . "&fields=latitude,longitude,city"); |
|
|
|
|
49
|
6 |
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
50
|
6 |
|
} |
51
|
|
|
|
52
|
6 |
|
private function geoExecuteCurl() |
53
|
|
|
{ |
54
|
6 |
|
$this->geoLocation = json_decode(curl_exec($this->curl)); |
|
|
|
|
55
|
6 |
|
} |
56
|
|
|
|
57
|
6 |
|
private function geoCloseCurl() |
58
|
|
|
{ |
59
|
6 |
|
curl_close($this->curl); |
|
|
|
|
60
|
6 |
|
} |
61
|
|
|
|
62
|
8 |
|
public function getGeoLocation() |
63
|
|
|
{ |
64
|
8 |
|
return $this->geoLocation; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function setGeoLocation($geoLocation) |
68
|
|
|
{ |
69
|
2 |
|
$this->geoLocation = $geoLocation; |
70
|
2 |
|
} |
71
|
|
|
|
72
|
8 |
|
public function geoLocationOK() |
73
|
|
|
{ |
74
|
|
|
|
75
|
8 |
|
if (!empty($this->geoLocation->longitude)) { |
|
|
|
|
76
|
2 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
return false; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|