1
|
|
|
<?php |
2
|
|
|
namespace Anax\Model; |
3
|
|
|
class Weather |
4
|
|
|
{ |
5
|
|
|
protected $locationiqKey; |
6
|
|
|
protected $darkskyKey; |
7
|
|
|
|
8
|
4 |
|
public function setApiKeys($locationiqKey, $darkskyKey) |
9
|
|
|
{ |
10
|
4 |
|
$this->locationiqKey = $locationiqKey; |
11
|
4 |
|
$this->darkskyKey = $darkskyKey; |
12
|
4 |
|
} |
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* @param $when -> 7 days in future, or 30 days in the past, $lat -> Latitude, $long -> Longitude |
|
|
|
|
16
|
|
|
* @return array of past weather |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
|
20
|
4 |
|
public function getLocationData($search) : array |
21
|
|
|
{ |
22
|
4 |
|
$url = "https://eu1.locationiq.com/v1/search.php?key={$this->locationiqKey}&q={$search}&format=json&limit=1"; |
23
|
4 |
|
$response = get_headers($url); |
24
|
4 |
|
if ($response[0] === 'HTTP/1.1 200 OK') { |
25
|
|
|
$details = json_decode(file_get_contents($url)); |
26
|
|
|
return [ |
27
|
|
|
"lat" => $details[0]->lat, |
28
|
|
|
"lon"=> $details[0]->lon, |
29
|
|
|
"error" => null, |
30
|
|
|
]; |
31
|
|
|
} else { |
32
|
|
|
return [ |
33
|
4 |
|
"error" => $search . " is not a city", |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
public function getWeather($when, $lat, $lon) : array |
39
|
|
|
{ |
40
|
|
|
$now = time(); |
41
|
|
|
$dates = array(); |
42
|
|
|
$weather = array(); |
43
|
|
|
$temp = array(); |
44
|
|
|
$time = array(); |
45
|
|
|
if ($when == "past") { |
46
|
|
|
for ($i = 0; $i < 30; $i++) { |
47
|
|
|
$now -= 86400; |
48
|
|
|
$dates[] = $now; |
49
|
|
|
} |
50
|
|
|
} elseif ($when == "future") { |
51
|
|
|
for ($i = 0; $i < 7; $i++) { |
52
|
|
|
$now += 86400; |
53
|
|
|
$dates[] = $now; |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
return [ |
57
|
|
|
"error" => "select a time", |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
for ($i = 0; $i < count($dates); $i++) { |
|
|
|
|
61
|
|
|
$details = json_decode(file_get_contents("https://api.darksky.net/forecast/{$this->darkskyKey}/{$lon},{$lat},{$dates[$i]}?lang=sv&units=si")); |
62
|
|
|
$time[] = date('Y-m-d', $details->currently->time); |
63
|
|
|
$weather[] = $details->currently->summary; |
64
|
|
|
$temp[] = $details->currently->temperature; |
65
|
|
|
} |
66
|
|
|
return [ |
67
|
|
|
"time" => $time, |
68
|
|
|
"weather" => $weather, |
69
|
|
|
"temp" => $temp, |
70
|
|
|
"error" => "no error", |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getWeatherMultiCurl($when, $lat, $lon) : array |
75
|
|
|
{ |
76
|
|
|
$dates = []; |
77
|
|
|
$now = time(); |
78
|
|
|
$url = "https://api.darksky.net/forecast/{$this->darkskyKey}/{$lon},{$lat}"; |
79
|
|
|
|
80
|
|
|
if ($when == "past") { |
81
|
|
|
for ($i = 0; $i < 30; $i++) { |
82
|
|
|
$now -= 86400; |
83
|
|
|
$dates[] = $now; |
84
|
|
|
} |
85
|
|
|
} elseif($when == "future") { |
86
|
|
|
for ($i = 0; $i < 7; $i++) { |
87
|
|
|
$now += 86400; |
88
|
|
|
$dates[] = $now; |
89
|
|
|
} |
90
|
|
|
} else { |
91
|
|
|
return [ |
92
|
|
|
"error" => "select a time", |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$mh = curl_multi_init(); |
97
|
|
|
$chAll = []; |
98
|
|
|
foreach ($dates as $days) { |
99
|
|
|
$ch = curl_init("$url,{$days}?lang=sv&units=si"); |
100
|
|
|
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true]); |
|
|
|
|
101
|
|
|
curl_multi_add_handle($mh, $ch); |
|
|
|
|
102
|
|
|
$chAll[] = $ch; |
103
|
|
|
} |
104
|
|
|
$running = null; |
105
|
|
|
do { |
106
|
|
|
curl_multi_exec($mh, $running); |
107
|
|
|
} while ($running); |
108
|
|
|
foreach ($chAll as $ch) { |
109
|
|
|
curl_multi_remove_handle($mh, $ch); |
110
|
|
|
} |
111
|
|
|
$response = []; |
112
|
|
|
foreach ($chAll as $ch) { |
113
|
|
|
$data = curl_multi_getcontent($ch); |
114
|
|
|
$response[] = json_decode($data, true); |
115
|
|
|
} |
116
|
|
|
return $response; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|