|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lii\Model; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Get weather reports for a specific location. |
|
7
|
|
|
* |
|
8
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
9
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateField) |
|
10
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
11
|
|
|
*/ |
|
12
|
|
|
class WeatherReport |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
// Global variables |
|
16
|
|
|
private $accessKey = ''; |
|
17
|
|
|
|
|
18
|
11 |
|
public function __construct($apiKey) |
|
19
|
|
|
{ |
|
20
|
11 |
|
$this->accessKey = $apiKey; |
|
21
|
11 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
// public function checkWeather($lat, $long) |
|
24
|
|
|
// { |
|
25
|
|
|
// // Initialize CURL: |
|
26
|
|
|
// $cRes = curl_init('api.openweathermap.org/data/2.5/weather?lat='.$lat.'&lon='.$long.'&units=metric&appid='.$this->accessKey.''); |
|
27
|
|
|
// curl_setopt($cRes, CURLOPT_RETURNTRANSFER, true); |
|
28
|
|
|
// |
|
29
|
|
|
// // Store the data: |
|
30
|
|
|
// $json = curl_exec($cRes); |
|
31
|
|
|
// curl_close($cRes); |
|
32
|
|
|
// |
|
33
|
|
|
// // Decode JSON response: |
|
34
|
|
|
// $result = json_decode($json, true); |
|
35
|
|
|
// |
|
36
|
|
|
// return $result; |
|
37
|
|
|
// } |
|
38
|
|
|
|
|
39
|
|
|
public function fetchAllWeather($lat, $long) |
|
40
|
|
|
{ |
|
41
|
|
|
// build the individual requests, but do not execute them |
|
42
|
|
|
$ch1 = curl_init('api.openweathermap.org/data/2.5/weather?lat='.$lat.'&lon='.$long.'&units=metric&appid='.$this->accessKey.''); |
|
43
|
|
|
$ch2 = curl_init('https://api.openweathermap.org/data/2.5/onecall?lat='.$lat.'&lon='.$long.'&exclude=hourly,minutely,current&units=metric&appid='.$this->accessKey.''); |
|
44
|
|
|
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
|
|
45
|
|
|
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); |
|
46
|
|
|
|
|
47
|
|
|
// build the multi-curl handle, adding both $ch |
|
48
|
|
|
$mh1 = curl_multi_init(); |
|
49
|
|
|
curl_multi_add_handle($mh1, $ch1); |
|
|
|
|
|
|
50
|
|
|
curl_multi_add_handle($mh1, $ch2); |
|
51
|
|
|
|
|
52
|
|
|
// execute all queries simultaneously, and continue when all are complete |
|
53
|
|
|
$running = null; |
|
54
|
|
|
do { |
|
55
|
|
|
curl_multi_exec($mh1, $running); |
|
56
|
|
|
} while ($running); |
|
57
|
|
|
|
|
58
|
|
|
//close the handles |
|
59
|
|
|
curl_multi_remove_handle($mh1, $ch1); |
|
|
|
|
|
|
60
|
|
|
curl_multi_remove_handle($mh1, $ch2); |
|
61
|
|
|
curl_multi_close($mh1); |
|
62
|
|
|
|
|
63
|
|
|
// all of our requests are done, we can now access the results |
|
64
|
|
|
$result1 = curl_multi_getcontent($ch1); |
|
|
|
|
|
|
65
|
|
|
$currentWeather = json_decode($result1, true); |
|
66
|
|
|
$result2 = curl_multi_getcontent($ch2); |
|
67
|
|
|
$forecastWeather = json_decode($result2, true); |
|
68
|
|
|
|
|
69
|
|
|
return [$currentWeather, $forecastWeather]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
public function getHistoricDates() |
|
73
|
|
|
{ |
|
74
|
2 |
|
$dates = []; |
|
75
|
2 |
|
$date = date_create(date("Y-m-d", time())); |
|
76
|
2 |
|
date_modify($date, "-1 days"); |
|
|
|
|
|
|
77
|
2 |
|
$date1 = date_create(date("Y-m-d", date_format($date, "U"))); |
|
|
|
|
|
|
78
|
2 |
|
date_modify($date, "-1 days"); |
|
79
|
2 |
|
$date2 = date_create(date("Y-m-d", date_format($date, "U"))); |
|
80
|
2 |
|
date_modify($date, "-1 days"); |
|
81
|
2 |
|
$date3 = date_create(date("Y-m-d", date_format($date, "U"))); |
|
82
|
2 |
|
date_modify($date, "-1 days"); |
|
83
|
2 |
|
$date4 = date_create(date("Y-m-d", date_format($date, "U"))); |
|
84
|
2 |
|
array_push($dates, date_format($date1, "U"), date_format($date2, "U"), date_format($date3, "U"), date_format($date4, "U")); |
|
85
|
|
|
|
|
86
|
2 |
|
return $dates; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getHistoricWeather($days, $lat, $long) |
|
90
|
|
|
{ |
|
91
|
|
|
$url = 'https://api.openweathermap.org/data/2.5/onecall/timemachine?lat='.$lat.'&lon='.$long.'&units=metric&appid='.$this->accessKey.'&dt='; |
|
92
|
|
|
|
|
93
|
|
|
$options = [ |
|
94
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
|
|
// Add all curl handlers and remember them |
|
98
|
|
|
// Initiate the multi curl handler |
|
99
|
|
|
$mh1 = curl_multi_init(); |
|
100
|
|
|
$chAll = []; |
|
101
|
|
|
foreach ($days as $day) { |
|
102
|
|
|
$ch1 = curl_init("$url$day"); |
|
103
|
|
|
curl_setopt_array($ch1, $options); |
|
|
|
|
|
|
104
|
|
|
curl_multi_add_handle($mh1, $ch1); |
|
|
|
|
|
|
105
|
|
|
$chAll[] = $ch1; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Execute all queries simultaneously, |
|
109
|
|
|
// and continue when all are complete |
|
110
|
|
|
$running = null; |
|
111
|
|
|
do { |
|
112
|
|
|
curl_multi_exec($mh1, $running); |
|
113
|
|
|
} while ($running); |
|
114
|
|
|
|
|
115
|
|
|
// Close the handles |
|
116
|
|
|
foreach ($chAll as $ch1) { |
|
117
|
|
|
curl_multi_remove_handle($mh1, $ch1); |
|
118
|
|
|
} |
|
119
|
|
|
curl_multi_close($mh1); |
|
120
|
|
|
|
|
121
|
|
|
// All of our requests are done, we can now access the results |
|
122
|
|
|
$response = []; |
|
123
|
|
|
foreach ($chAll as $ch1) { |
|
124
|
|
|
$data = curl_multi_getcontent($ch1); |
|
125
|
|
|
$response[] = json_decode($data, true); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $response; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function getJsonWeather($days, $lat, $long) |
|
132
|
|
|
{ |
|
133
|
|
|
$allWeather = $this->fetchAllWeather($lat, $long); |
|
134
|
|
|
$historicWeather = $this->getHistoricWeather($days, $lat, $long); |
|
135
|
|
|
|
|
136
|
|
|
// var_dump($historicWeather); |
|
137
|
|
|
|
|
138
|
|
|
$json = [ |
|
139
|
|
|
"city" => "{$allWeather[0]['name']}", |
|
140
|
|
|
"current" => [ |
|
141
|
|
|
"weather" => "{$allWeather[0]['weather'][0]['description']}", |
|
142
|
|
|
"temp" => "{$allWeather[0]['main']['temp']}", |
|
143
|
|
|
"wind_speed" => "{$allWeather[0]['wind']['speed']}", |
|
144
|
|
|
], |
|
145
|
|
|
"forecast" => [ |
|
146
|
|
|
[ |
|
147
|
|
|
"date" => "{$allWeather[1]['daily'][0]['dt']}", |
|
148
|
|
|
"weather" => "{$allWeather[1]['daily'][0]['weather'][0]['main']}", |
|
149
|
|
|
"temp" => "{$allWeather[1]['daily'][0]['temp']['day']}", |
|
150
|
|
|
"wind_speed" => "{$allWeather[1]['daily'][0]['wind_speed']}", |
|
151
|
|
|
], |
|
152
|
|
|
[ |
|
153
|
|
|
"date" => "{$allWeather[1]['daily'][1]['dt']}", |
|
154
|
|
|
"weather" => "{$allWeather[1]['daily'][1]['weather'][0]['main']}", |
|
155
|
|
|
"temp" => "{$allWeather[1]['daily'][1]['temp']['day']}", |
|
156
|
|
|
"wind_speed" => "{$allWeather[1]['daily'][1]['wind_speed']}", |
|
157
|
|
|
], |
|
158
|
|
|
[ |
|
159
|
|
|
"date" => "{$allWeather[1]['daily'][2]['dt']}", |
|
160
|
|
|
"weather" => "{$allWeather[1]['daily'][2]['weather'][0]['main']}", |
|
161
|
|
|
"temp" => "{$allWeather[1]['daily'][2]['temp']['day']}", |
|
162
|
|
|
"wind_speed" => "{$allWeather[1]['daily'][2]['wind_speed']}", |
|
163
|
|
|
], |
|
164
|
|
|
[ |
|
165
|
|
|
"date" => "{$allWeather[1]['daily'][3]['dt']}", |
|
166
|
|
|
"weather" => "{$allWeather[1]['daily'][3]['weather'][0]['main']}", |
|
167
|
|
|
"temp" => "{$allWeather[1]['daily'][3]['temp']['day']}", |
|
168
|
|
|
"wind_speed" => "{$allWeather[1]['daily'][3]['wind_speed']}", |
|
169
|
|
|
], |
|
170
|
|
|
[ |
|
171
|
|
|
"date" => "{$allWeather[1]['daily'][4]['dt']}", |
|
172
|
|
|
"weather" => "{$allWeather[1]['daily'][4]['weather'][0]['main']}", |
|
173
|
|
|
"temp" => "{$allWeather[1]['daily'][4]['temp']['day']}", |
|
174
|
|
|
"wind_speed" => "{$allWeather[1]['daily'][4]['wind_speed']}", |
|
175
|
|
|
], |
|
176
|
|
|
[ |
|
177
|
|
|
"date" => "{$allWeather[1]['daily'][5]['dt']}", |
|
178
|
|
|
"weather" => "{$allWeather[1]['daily'][5]['weather'][0]['main']}", |
|
179
|
|
|
"temp" => "{$allWeather[1]['daily'][5]['temp']['day']}", |
|
180
|
|
|
"wind_speed" => "{$allWeather[1]['daily'][5]['wind_speed']}", |
|
181
|
|
|
], |
|
182
|
|
|
[ |
|
183
|
|
|
"date" => "{$allWeather[1]['daily'][6]['dt']}", |
|
184
|
|
|
"weather" => "{$allWeather[1]['daily'][6]['weather'][0]['main']}", |
|
185
|
|
|
"temp" => "{$allWeather[1]['daily'][6]['temp']['day']}", |
|
186
|
|
|
"wind_speed" => "{$allWeather[1]['daily'][6]['wind_speed']}", |
|
187
|
|
|
], |
|
188
|
|
|
[ |
|
189
|
|
|
"date" => "{$allWeather[1]['daily'][7]['dt']}", |
|
190
|
|
|
"weather" => "{$allWeather[1]['daily'][7]['weather'][0]['main']}", |
|
191
|
|
|
"temp" => "{$allWeather[1]['daily'][7]['temp']['day']}", |
|
192
|
|
|
"wind_speed" => "{$allWeather[1]['daily'][7]['wind_speed']}", |
|
193
|
|
|
] |
|
194
|
|
|
], |
|
195
|
|
|
"historical" => [ |
|
196
|
|
|
[ |
|
197
|
|
|
"date" => "{$historicWeather[0]['current']['dt']}", |
|
198
|
|
|
"weather" => "{$historicWeather[0]['current']['weather'][0]['main']}", |
|
199
|
|
|
"temp" => "{$historicWeather[0]['current']['temp']}", |
|
200
|
|
|
"wind_speed" => "{$historicWeather[0]['current']['wind_speed']}", |
|
201
|
|
|
], |
|
202
|
|
|
[ |
|
203
|
|
|
"date" => "{$historicWeather[1]['current']['dt']}", |
|
204
|
|
|
"weather" => "{$historicWeather[1]['current']['weather'][0]['main']}", |
|
205
|
|
|
"temp" => "{$historicWeather[1]['current']['temp']}", |
|
206
|
|
|
"wind_speed" => "{$historicWeather[1]['current']['wind_speed']}", |
|
207
|
|
|
], |
|
208
|
|
|
[ |
|
209
|
|
|
"date" => "{$historicWeather[2]['current']['dt']}", |
|
210
|
|
|
"weather" => "{$historicWeather[2]['current']['weather'][0]['main']}", |
|
211
|
|
|
"temp" => "{$historicWeather[2]['current']['temp']}", |
|
212
|
|
|
"wind_speed" => "{$historicWeather[2]['current']['wind_speed']}", |
|
213
|
|
|
], |
|
214
|
|
|
[ |
|
215
|
|
|
"date" => "{$historicWeather[3]['current']['dt']}", |
|
216
|
|
|
"weather" => "{$historicWeather[3]['current']['weather'][0]['main']}", |
|
217
|
|
|
"temp" => "{$historicWeather[3]['current']['temp']}", |
|
218
|
|
|
"wind_speed" => "{$historicWeather[3]['current']['wind_speed']}", |
|
219
|
|
|
], |
|
220
|
|
|
] |
|
221
|
|
|
]; |
|
222
|
|
|
|
|
223
|
|
|
return [$json]; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|