1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// namespace Anax\Weather; |
4
|
|
|
namespace Kris3XIQ\Weather; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A model class retrieving data from an external server. |
8
|
|
|
* |
9
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
10
|
|
|
*/ |
11
|
|
|
class Weather |
12
|
|
|
{ |
13
|
|
|
protected $key; |
14
|
|
|
protected $keyChain; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Set API-key value. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function setApiKey(string $apiKey) |
22
|
|
|
{ |
23
|
|
|
$this->key = $apiKey; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set a new API-keychain (multiple API-keys), if you |
28
|
|
|
* need to fetch multiple API's. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function setKeyChain(array $keyChain) |
33
|
|
|
{ |
34
|
|
|
$this->keyChain["keys"] = $keyChain; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function verifyLocation(string $input) |
38
|
|
|
{ |
39
|
|
|
$apiKey; |
|
|
|
|
40
|
|
|
$keyChain = $this->keyChain; |
41
|
|
|
if (array_key_exists("keys", $keyChain)) { |
42
|
|
|
$apiKey = $keyChain["keys"]["openweathermap"]; |
43
|
|
|
} |
44
|
|
|
$apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=$input&units=metric&appid=$apiKey"; |
|
|
|
|
45
|
|
|
|
46
|
|
|
$ch = curl_init(); |
47
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
48
|
|
|
curl_setopt($ch, CURLOPT_URL, $apiUrl); |
49
|
|
|
|
50
|
|
|
$data = curl_exec($ch); |
|
|
|
|
51
|
|
|
|
52
|
|
|
curl_close($ch); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$status = json_decode($data, true); |
55
|
|
|
|
56
|
|
|
return $status["cod"]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getWeatherCurrentFromAPI(string $input) |
60
|
|
|
{ |
61
|
|
|
$apiKey; |
|
|
|
|
62
|
|
|
$keyChain = $this->keyChain; |
63
|
|
|
if (array_key_exists("keys", $keyChain)) { |
64
|
|
|
$apiKey = $keyChain["keys"]["openweathermap"]; |
65
|
|
|
} |
66
|
|
|
$apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=$input&units=metric&appid=$apiKey"; |
|
|
|
|
67
|
|
|
|
68
|
|
|
$ch = curl_init(); |
69
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
70
|
|
|
curl_setopt($ch, CURLOPT_URL, $apiUrl); |
71
|
|
|
|
72
|
|
|
$data = curl_exec($ch); |
|
|
|
|
73
|
|
|
|
74
|
|
|
curl_close($ch); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$weatherInfo = json_decode($data, true); |
77
|
|
|
|
78
|
|
|
return $weatherInfo; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getWeatherHistoryFromAPI($lat, $lon) |
82
|
|
|
{ |
83
|
|
|
$apiKey; |
|
|
|
|
84
|
|
|
$keyChain = $this->keyChain; |
85
|
|
|
if (array_key_exists("keys", $keyChain)) { |
86
|
|
|
$apiKey = $keyChain["keys"]["openweathermap"]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Create all cURL resources |
90
|
|
|
$dayOne = curl_init(); |
91
|
|
|
$dayTwo = curl_init(); |
92
|
|
|
$dayThree = curl_init(); |
93
|
|
|
$dayFour = curl_init(); |
94
|
|
|
$dayFive = curl_init(); |
95
|
|
|
|
96
|
|
|
// Set dates |
97
|
|
|
$dayMinusOne = mktime(0, 0, 0, date("m"), date("d"), date("Y")); |
|
|
|
|
98
|
|
|
$dayMinusTwo = mktime(0, 0, 0, date("m"), date("d")-1, date("Y")); |
99
|
|
|
$dayMinusThree = mktime(0, 0, 0, date("m"), date("d")-2, date("Y")); |
100
|
|
|
$dayMinusFour = mktime(0, 0, 0, date("m"), date("d")-3, date("Y")); |
101
|
|
|
$dayMinusFive = mktime(0, 0, 0, date("m"), date("d")-4, date("Y")); |
102
|
|
|
|
103
|
|
|
// set URL and other appropriate options |
104
|
|
|
curl_setopt($dayOne, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusOne&units=metric&appid=$apiKey"); |
|
|
|
|
105
|
|
|
curl_setopt($dayOne, CURLOPT_RETURNTRANSFER, true); |
106
|
|
|
curl_setopt($dayTwo, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusTwo&units=metric&appid=$apiKey"); |
107
|
|
|
curl_setopt($dayTwo, CURLOPT_RETURNTRANSFER, true); |
108
|
|
|
curl_setopt($dayThree, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusThree&units=metric&appid=$apiKey"); |
109
|
|
|
curl_setopt($dayThree, CURLOPT_RETURNTRANSFER, true); |
110
|
|
|
curl_setopt($dayFour, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusFour&units=metric&appid=$apiKey"); |
111
|
|
|
curl_setopt($dayFour, CURLOPT_RETURNTRANSFER, true); |
112
|
|
|
curl_setopt($dayFive, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusFive&units=metric&appid=$apiKey"); |
113
|
|
|
curl_setopt($dayFive, CURLOPT_RETURNTRANSFER, true); |
114
|
|
|
|
115
|
|
|
// Create the multiple cURL handle |
116
|
|
|
$mh = curl_multi_init(); |
117
|
|
|
|
118
|
|
|
// Add the five handles |
119
|
|
|
curl_multi_add_handle($mh, $dayOne); |
|
|
|
|
120
|
|
|
curl_multi_add_handle($mh, $dayTwo); |
121
|
|
|
curl_multi_add_handle($mh, $dayThree); |
122
|
|
|
curl_multi_add_handle($mh, $dayFour); |
123
|
|
|
curl_multi_add_handle($mh, $dayFive); |
124
|
|
|
|
125
|
|
|
do { |
126
|
|
|
$status = curl_multi_exec($mh, $active); |
127
|
|
|
if ($active) { |
128
|
|
|
curl_multi_select($mh); |
129
|
|
|
} |
130
|
|
|
} while ($active && $status == CURLM_OK); |
131
|
|
|
|
132
|
|
|
curl_multi_remove_handle($mh, $dayOne); |
|
|
|
|
133
|
|
|
curl_multi_remove_handle($mh, $dayTwo); |
134
|
|
|
curl_multi_remove_handle($mh, $dayThree); |
135
|
|
|
curl_multi_remove_handle($mh, $dayFour); |
136
|
|
|
curl_multi_remove_handle($mh, $dayFive); |
137
|
|
|
curl_multi_close($mh); |
138
|
|
|
|
139
|
|
|
$data; |
|
|
|
|
140
|
|
|
$response1 = curl_multi_getcontent($dayOne); |
|
|
|
|
141
|
|
|
$response2 = curl_multi_getcontent($dayTwo); |
142
|
|
|
$response3 = curl_multi_getcontent($dayThree); |
143
|
|
|
$response4 = curl_multi_getcontent($dayFour); |
144
|
|
|
$response5 = curl_multi_getcontent($dayFive); |
145
|
|
|
$data = [ |
146
|
|
|
"cod" => 200, |
147
|
|
|
"past_days" => [ |
148
|
|
|
"past_01" => $response1, |
149
|
|
|
"past_02" => $response2, |
150
|
|
|
"past_03" => $response3, |
151
|
|
|
"past_04" => $response4, |
152
|
|
|
"past_05" => $response5 |
153
|
|
|
], |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
return $data; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function ipTrackWithGeolocation(string $input) |
160
|
|
|
{ |
161
|
|
|
$apiKey; |
|
|
|
|
162
|
|
|
$keyChain = $this->keyChain; |
163
|
|
|
if (array_key_exists("keys", $keyChain)) { |
164
|
|
|
$apiKey = $keyChain["keys"]["ipstack"]; |
165
|
|
|
} |
166
|
|
|
$apiUrl = "http://api.ipstack.com/$input?access_key=$apiKey"; |
|
|
|
|
167
|
|
|
|
168
|
|
|
$ch = curl_init(); |
169
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
170
|
|
|
curl_setopt($ch, CURLOPT_URL, $apiUrl); |
171
|
|
|
|
172
|
|
|
$data = curl_exec($ch); |
|
|
|
|
173
|
|
|
|
174
|
|
|
curl_close($ch); |
|
|
|
|
175
|
|
|
|
176
|
|
|
$data = json_decode($data, true); |
177
|
|
|
|
178
|
|
|
return $data; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Confirm validity of ipAddress to be either |
183
|
|
|
* IPv4 or IPv6. |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function isIpAddress($ip) |
188
|
|
|
{ |
189
|
|
|
$ipv4 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); |
190
|
|
|
$ipv6 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); |
191
|
|
|
if ($ipv4 || $ipv6) { |
192
|
|
|
return true; |
|
|
|
|
193
|
|
|
} else { |
194
|
|
|
return false; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Converts current weather- and history data to JSON |
200
|
|
|
* |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
public function convertToJSON($currentWeather, $historyWeather) |
204
|
|
|
{ |
205
|
|
|
if ($historyWeather) { |
206
|
|
|
$pastOne = json_decode($historyWeather["past_days"]["past_01"], true); |
207
|
|
|
$pastTwo = json_decode($historyWeather["past_days"]["past_02"], true); |
208
|
|
|
$pastThree = json_decode($historyWeather["past_days"]["past_03"], true); |
209
|
|
|
$pastFour = json_decode($historyWeather["past_days"]["past_04"], true); |
210
|
|
|
$pastFive = json_decode($historyWeather["past_days"]["past_05"], true); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$dataJSON = [ |
214
|
|
|
"status code" => $currentWeather["cod"], |
215
|
|
|
"name" => $currentWeather["name"] . ", " . $currentWeather["sys"]["country"], |
216
|
|
|
"current_weather" => [ |
217
|
|
|
"main" => $currentWeather["weather"][0]["main"], |
218
|
|
|
"description" => $currentWeather["weather"][0]["description"], |
219
|
|
|
"temperature" => $currentWeather["main"]["temp"], |
220
|
|
|
"feels_like" => $currentWeather["main"]["feels_like"], |
221
|
|
|
], |
222
|
|
|
"yesterday" => [ |
223
|
|
|
"date" => gmdate('r', $pastOne["hourly"][14]["dt"]), |
|
|
|
|
224
|
|
|
"main" => $pastOne["hourly"][14]["weather"][0]["main"], |
225
|
|
|
"description" => $pastOne["hourly"][14]["weather"][0]["description"], |
226
|
|
|
"temperature" => $pastOne["hourly"][14]["temp"], |
227
|
|
|
"feels_like" => $pastOne["hourly"][14]["feels_like"], |
228
|
|
|
], |
229
|
|
|
"2_days_ago" => [ |
230
|
|
|
"date" => gmdate('r', $pastTwo["hourly"][14]["dt"]), |
|
|
|
|
231
|
|
|
"main" => $pastTwo["hourly"][14]["weather"][0]["main"], |
232
|
|
|
"description" => $pastTwo["hourly"][14]["weather"][0]["description"], |
233
|
|
|
"temperature" => $pastTwo["hourly"][14]["temp"], |
234
|
|
|
"feels_like" => $pastTwo["hourly"][14]["feels_like"], |
235
|
|
|
], |
236
|
|
|
"3_days_ago" => [ |
237
|
|
|
"date" => gmdate('r', $pastThree["hourly"][14]["dt"]), |
|
|
|
|
238
|
|
|
"main" => $pastThree["hourly"][14]["weather"][0]["main"], |
239
|
|
|
"description" => $pastThree["hourly"][14]["weather"][0]["description"], |
240
|
|
|
"temperature" => $pastThree["hourly"][14]["temp"], |
241
|
|
|
"feels_like" => $pastThree["hourly"][14]["feels_like"], |
242
|
|
|
], |
243
|
|
|
"4_days_ago" => [ |
244
|
|
|
"date" => gmdate('r', $pastFour["hourly"][14]["dt"]), |
|
|
|
|
245
|
|
|
"main" => $pastFour["hourly"][14]["weather"][0]["main"], |
246
|
|
|
"description" => $pastFour["hourly"][14]["weather"][0]["description"], |
247
|
|
|
"temperature" => $pastFour["hourly"][14]["temp"], |
248
|
|
|
"feels_like" => $pastFour["hourly"][14]["feels_like"], |
249
|
|
|
], |
250
|
|
|
"5_days_ago" => [ |
251
|
|
|
"date" => gmdate('r', $pastFive["hourly"][14]["dt"]), |
|
|
|
|
252
|
|
|
"main" => $pastFive["hourly"][14]["weather"][0]["main"], |
253
|
|
|
"description" => $pastFive["hourly"][14]["weather"][0]["description"], |
254
|
|
|
"temperature" => $pastFive["hourly"][14]["temp"], |
255
|
|
|
"feels_like" => $pastFive["hourly"][14]["feels_like"], |
256
|
|
|
] |
257
|
|
|
]; |
258
|
|
|
|
259
|
|
|
return $dataJSON; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|