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 | 1 | $lon = $geoLocation->getGeoLocation()->longitude; |
|||
37 | 1 | $lat = $geoLocation->getGeoLocation()->latitude; |
|||
38 | 1 | $this->geoInitCurl(); |
|||
39 | 1 | $this->geoSetOptCurl($lat, $lon); |
|||
40 | 1 | $this->geoExecuteCurl(); |
|||
41 | 1 | $this->geoCloseCurl(); |
|||
42 | } |
||||
43 | 7 | } |
|||
44 | |||||
45 | 7 | public function checkWeatherMulti(object $geoLocation) |
|||
46 | { |
||||
47 | 7 | if ($geoLocation->geoLocationOK() === true) { |
|||
48 | # code... |
||||
49 | 1 | $lon = $geoLocation->getGeoLocation()->longitude; |
|||
50 | 1 | $lat = $geoLocation->getGeoLocation()->latitude; |
|||
51 | |||||
52 | 1 | $multiRequests = []; |
|||
53 | |||||
54 | 1 | for ($i = 0; $i < 5; $i++) { |
|||
55 | 1 | $unixTime = time() - ($i * 24 * 60 * 60); |
|||
56 | |||||
57 | 1 | $multiRequests[] = 'https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=' . $lat . '&lon=' . $lon . '&dt=' . $unixTime . '&units=metric&appid=' . $this->apiKey; |
|||
58 | } |
||||
59 | |||||
60 | 1 | $multiHandle = curl_multi_init(); |
|||
61 | 1 | $curlArray = array(); |
|||
62 | |||||
63 | 1 | foreach ($multiRequests as $i => $url) { |
|||
64 | 1 | $curlArray[$i] = curl_init($url); |
|||
65 | 1 | curl_setopt($curlArray[$i], CURLOPT_RETURNTRANSFER, true); |
|||
66 | 1 | curl_multi_add_handle($multiHandle, $curlArray[$i]); |
|||
67 | } |
||||
68 | |||||
69 | 1 | $running = null; |
|||
70 | do { |
||||
71 | 1 | usleep(10000); |
|||
72 | 1 | curl_multi_exec($multiHandle, $running); |
|||
73 | 1 | } while ($running > 0); |
|||
74 | |||||
75 | 1 | $res = array(); |
|||
76 | 1 | foreach ($multiRequests as $i => $url) { |
|||
77 | 1 | $res[$i] = json_decode(curl_multi_getcontent($curlArray[$i])); |
|||
78 | } |
||||
79 | |||||
80 | 1 | foreach ($multiRequests as $i => $url) { |
|||
81 | 1 | curl_multi_remove_handle($multiHandle, $curlArray[$i]); |
|||
82 | } |
||||
83 | 1 | curl_multi_close($multiHandle); |
|||
84 | 1 | return $res; |
|||
85 | } |
||||
86 | 6 | } |
|||
87 | |||||
88 | 1 | private function geoInitCurl() |
|||
89 | { |
||||
90 | 1 | $this->curl = curl_init(); |
|||
91 | 1 | } |
|||
92 | |||||
93 | 1 | private function geoSetOptCurl($latitude, $longitude) |
|||
94 | { |
||||
95 | |||||
96 | 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); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
97 | 1 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
|||
98 | 1 | } |
|||
99 | |||||
100 | 1 | private function geoExecuteCurl() |
|||
101 | { |
||||
102 | 1 | $this->geoLocation = json_decode(curl_exec($this->curl)); |
|||
0 ignored issues
–
show
$this->curl of type string is incompatible with the type resource expected by parameter $ch of curl_exec() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
103 | 1 | } |
|||
104 | |||||
105 | 1 | private function geoCloseCurl() |
|||
106 | { |
||||
107 | 1 | curl_close($this->curl); |
|||
0 ignored issues
–
show
$this->curl of type string is incompatible with the type resource expected by parameter $ch of curl_close() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
108 | 1 | } |
|||
109 | |||||
110 | 7 | public function getWeather() |
|||
111 | { |
||||
112 | 7 | return $this->geoLocation; |
|||
113 | } |
||||
114 | } |
||||
115 |