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 WeatherRequest implements ContainerInjectableInterface |
19
|
|
|
{ |
20
|
|
|
use ContainerInjectableTrait; |
21
|
|
|
|
22
|
|
|
private $curl = ""; |
23
|
|
|
private $apiKey = ""; |
24
|
|
|
private $geoLocation = []; |
25
|
|
|
|
26
|
6 |
|
public function setAPI(string $key) |
27
|
|
|
{ |
28
|
6 |
|
$this->apiKey = $this->di->get("keystore")->getKey($key); |
29
|
6 |
|
} |
30
|
|
|
|
31
|
7 |
|
public function checkWeather(object $geoLocation) |
32
|
|
|
{ |
33
|
|
|
// var_dump($geoLocation->geoLocationOK()); |
34
|
|
|
// var_dump($geoLocation->getGeoLocation()["latitude"]); |
35
|
7 |
|
if ($geoLocation->geoLocationOK() === true) { |
36
|
|
|
|
37
|
1 |
|
$lon = $geoLocation->getGeoLocation()->longitude; |
38
|
1 |
|
$lat = $geoLocation->getGeoLocation()->latitude; |
39
|
1 |
|
$this->geoInitCurl(); |
40
|
1 |
|
$this->geoSetOptCurl($lat, $lon); |
41
|
1 |
|
$this->geoExecuteCurl(); |
42
|
1 |
|
$this->geoCloseCurl(); |
43
|
|
|
} |
44
|
7 |
|
} |
45
|
|
|
|
46
|
7 |
|
public function checkWeatherMulti(object $geoLocation) |
47
|
|
|
{ |
48
|
7 |
|
if ($geoLocation->geoLocationOK() === true) { |
49
|
|
|
# code... |
50
|
1 |
|
$lon = $geoLocation->getGeoLocation()->longitude; |
51
|
1 |
|
$lat = $geoLocation->getGeoLocation()->latitude; |
52
|
|
|
|
53
|
1 |
|
$multiRequests = []; |
54
|
|
|
|
55
|
1 |
|
for ($i = 0; $i < 5; $i++) { |
56
|
1 |
|
$unixTime = time() - ($i * 24 * 60 * 60); |
57
|
|
|
|
58
|
1 |
|
$multiRequests[] = 'https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=' . $lat . '&lon=' . $lon . '&dt=' . $unixTime . '&units=metric&appid=' . $this->apiKey; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$multiHandle = curl_multi_init(); |
62
|
1 |
|
$curlArray = array(); |
63
|
|
|
|
64
|
1 |
|
foreach ($multiRequests as $i => $url) { |
65
|
1 |
|
$curlArray[$i] = curl_init($url); |
66
|
1 |
|
curl_setopt($curlArray[$i], CURLOPT_RETURNTRANSFER, true); |
67
|
1 |
|
curl_multi_add_handle($multiHandle, $curlArray[$i]); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
$running = null; |
71
|
|
|
do { |
72
|
1 |
|
usleep(10000); |
73
|
1 |
|
curl_multi_exec($multiHandle, $running); |
74
|
1 |
|
} while ($running > 0); |
75
|
|
|
|
76
|
1 |
|
$res = array(); |
77
|
1 |
|
foreach ($multiRequests as $i => $url) { |
78
|
1 |
|
$res[$i] = json_decode(curl_multi_getcontent($curlArray[$i])); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
foreach ($multiRequests as $i => $url) { |
82
|
1 |
|
curl_multi_remove_handle($multiHandle, $curlArray[$i]); |
83
|
|
|
} |
84
|
1 |
|
curl_multi_close($multiHandle); |
85
|
1 |
|
return $res; |
86
|
|
|
} |
87
|
6 |
|
} |
88
|
|
|
|
89
|
1 |
|
private function geoInitCurl() |
90
|
|
|
{ |
91
|
1 |
|
$this->curl = curl_init(); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
1 |
|
private function geoSetOptCurl($latitude, $longitude) |
95
|
|
|
{ |
96
|
|
|
|
97
|
1 |
|
curl_setopt($this->curl, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall?lat=" . $latitude . "&lon=" . $longitude . "&units=metric&exclude=hourly,minutely&appid=" . $this->apiKey); |
|
|
|
|
98
|
1 |
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
private function geoExecuteCurl() |
102
|
|
|
{ |
103
|
1 |
|
$this->geoLocation = json_decode(curl_exec($this->curl)); |
|
|
|
|
104
|
1 |
|
} |
105
|
|
|
|
106
|
1 |
|
private function geoCloseCurl() |
107
|
|
|
{ |
108
|
1 |
|
curl_close($this->curl); |
|
|
|
|
109
|
1 |
|
} |
110
|
|
|
|
111
|
7 |
|
public function getWeather() |
112
|
|
|
{ |
113
|
7 |
|
return $this->geoLocation; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|