1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Weather; |
4
|
|
|
|
5
|
|
|
class Weather |
6
|
|
|
{ |
7
|
|
|
private $key; |
8
|
|
|
|
9
|
9 |
|
public function setApiKey($key) |
10
|
|
|
{ |
11
|
9 |
|
$this->key = $key["config"]["key"]; |
12
|
9 |
|
return $this; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
|
16
|
7 |
|
public function getWeather($lat, $lon) |
17
|
|
|
{ |
18
|
|
|
|
19
|
7 |
|
$muH = curl_multi_init(); |
20
|
7 |
|
$res = []; |
21
|
7 |
|
$handles = []; |
22
|
7 |
|
$handles[0] = curl_init(); |
23
|
7 |
|
curl_setopt($handles[0], CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=hourly,minutely&appid={$this->key}&units=metric&lang=sv"); |
|
|
|
|
24
|
7 |
|
curl_setopt($handles[0], CURLOPT_RETURNTRANSFER, true); |
25
|
7 |
|
curl_setopt($handles[0], CURLOPT_HEADER, false); |
26
|
7 |
|
curl_multi_add_handle($muH, $handles[0]); |
|
|
|
|
27
|
|
|
|
28
|
7 |
|
for ($i = 1; $i < 6; $i++) { |
29
|
7 |
|
$handles[$i] = curl_init(); |
30
|
7 |
|
$days = "-${i} days"; |
31
|
7 |
|
$time = date(strtotime($days)); |
32
|
7 |
|
curl_setopt($handles[$i], CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=${lat}&lon=${lon}&dt=${time}&exclude=hourly,minutely&appid={$this->key}&units=metric&lang=sv"); |
33
|
7 |
|
curl_setopt($handles[$i], CURLOPT_RETURNTRANSFER, true); |
34
|
7 |
|
curl_setopt($handles[$i], CURLOPT_HEADER, false); |
35
|
7 |
|
curl_multi_add_handle($muH, $handles[$i]); |
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
$running = 0; |
39
|
|
|
do { |
40
|
7 |
|
\curl_multi_exec($muH, $running); |
41
|
7 |
|
} while ($running > 0); |
42
|
|
|
|
43
|
7 |
|
foreach ($handles as $i => $handle) { |
44
|
7 |
|
$res[$i] = \json_decode(\curl_multi_getcontent($handle), true); |
45
|
7 |
|
\curl_multi_remove_handle($muH, $handle); |
46
|
|
|
} |
47
|
|
|
|
48
|
7 |
|
\curl_multi_close($muH); |
49
|
|
|
|
50
|
7 |
|
return $res; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|