1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\WeatherAPI; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateInterval; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Showing off a standard class with methods and properties. |
10
|
|
|
* |
11
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
12
|
|
|
*/ |
13
|
|
|
class WeatherJSONModel |
14
|
|
|
{ |
15
|
|
|
protected $apikey1; |
16
|
|
|
protected $apikey2; |
17
|
|
|
protected $apikey3; |
18
|
|
|
|
19
|
|
|
/* |
20
|
|
|
* Sets the api keys from a file |
21
|
|
|
*/ |
22
|
7 |
|
public function __construct() |
23
|
|
|
{ |
24
|
7 |
|
$apiKeys = require ANAX_INSTALL_PATH . "/config/api_keys.php"; |
25
|
7 |
|
$this->apikey1 = $apiKeys["ipstacks"]; |
26
|
7 |
|
$this->apikey2 = $apiKeys["darksky"]; |
27
|
7 |
|
$this->apikey3 = $apiKeys["geocode"]; |
28
|
7 |
|
} |
29
|
|
|
|
30
|
|
|
/* |
31
|
|
|
* Using nominatim to geo search an adress |
32
|
|
|
*/ |
33
|
4 |
|
public function geocode($address) |
34
|
|
|
{ |
35
|
4 |
|
$adrsurl = urlencode($address); |
36
|
4 |
|
$json = []; |
|
|
|
|
37
|
|
|
|
38
|
|
|
// Initialize CURL: |
39
|
4 |
|
$ch = curl_init("https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q={$adrsurl}&limit=1&[email protected]"); |
40
|
4 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
41
|
|
|
|
42
|
|
|
// Store the data: |
43
|
4 |
|
$json = curl_exec($ch); |
|
|
|
|
44
|
4 |
|
curl_close($ch); |
|
|
|
|
45
|
|
|
|
46
|
|
|
// Decode JSON response: |
47
|
4 |
|
$apiResult = json_decode($json, true); |
48
|
|
|
|
49
|
4 |
|
if ($apiResult) { |
50
|
|
|
return [ |
51
|
3 |
|
"lat" => $apiResult[0]['lat'], |
52
|
3 |
|
"long" => $apiResult[0]['lon'], |
53
|
3 |
|
"city" => $apiResult[0]["address"]["city"] ?? "", |
54
|
3 |
|
"region" => $apiResult[0]["address"]["state"] ?? "", |
55
|
3 |
|
"country" => $apiResult[0]["address"]["country"] ?? "" |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return [ |
60
|
2 |
|
"404" => "Error: Couldnt find anything with that search!" |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/* |
65
|
|
|
* Using ipstacks to geo search an IP adress |
66
|
|
|
*/ |
67
|
2 |
|
public function ipCurl($ipAdress) |
68
|
|
|
{ |
69
|
2 |
|
$json = []; |
|
|
|
|
70
|
|
|
|
71
|
|
|
// Initialize CURL: |
72
|
2 |
|
$ch = curl_init('http://api.ipstack.com/'. $ipAdress . '?access_key=' . $this->apikey1 . ''); |
73
|
2 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
74
|
|
|
|
75
|
|
|
// Store the data: |
76
|
2 |
|
$json = curl_exec($ch); |
|
|
|
|
77
|
2 |
|
curl_close($ch); |
|
|
|
|
78
|
|
|
|
79
|
|
|
// Decode JSON response: |
80
|
2 |
|
$apiResult = json_decode($json, true); |
81
|
|
|
|
82
|
|
|
return [ |
83
|
2 |
|
"lat" => $apiResult["latitude"], |
84
|
2 |
|
"long" => $apiResult["longitude"], |
85
|
2 |
|
"city" => $apiResult["city"] ?? $apiResult["address"]["village"], |
86
|
2 |
|
"region" => $apiResult["region_name"], |
87
|
2 |
|
"country" => $apiResult["country_name"] |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/* |
92
|
|
|
* Fetching weather |
93
|
|
|
*/ |
94
|
3 |
|
public function fetchCurrentWeather($coords) |
95
|
|
|
{ |
96
|
3 |
|
$json = []; |
|
|
|
|
97
|
3 |
|
$location = $coords["lat"] . ',' . $coords["long"]; |
98
|
|
|
|
99
|
|
|
// Initialize CURL: |
100
|
3 |
|
$ch = curl_init('https://api.darksky.net/forecast/'.$this->apikey2.'/'.$location.'?exclude=minutely,hourly,currently,alerts,flags&extend=daily&lang=sv&units=ca'); |
101
|
3 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
102
|
|
|
|
103
|
|
|
// Store the data: |
104
|
3 |
|
$json = curl_exec($ch); |
|
|
|
|
105
|
3 |
|
curl_close($ch); |
|
|
|
|
106
|
|
|
|
107
|
|
|
// Decode JSON response: |
108
|
3 |
|
$apiResult = json_decode($json, true); |
109
|
|
|
|
110
|
3 |
|
return [$apiResult]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/* |
114
|
|
|
* Fetching weather |
115
|
|
|
*/ |
116
|
2 |
|
public function fetchPrevWeather($coords, $days) |
117
|
|
|
{ |
118
|
2 |
|
$json = []; |
119
|
2 |
|
$res = $this->multiCurl($coords, $days); |
120
|
2 |
|
foreach ($res as $day) { |
121
|
2 |
|
$json[] = $day["daily"]["data"]; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
return [$json]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/* |
128
|
|
|
* Method for setting up multi curl |
129
|
|
|
*/ |
130
|
2 |
|
public function multiCurl($coords, $days) |
131
|
|
|
{ |
132
|
2 |
|
$urls = $this->getUrls($coords, $days); |
133
|
|
|
|
134
|
2 |
|
$mCurl = curl_multi_init(); |
135
|
2 |
|
$handles = []; |
136
|
2 |
|
$json = []; |
137
|
2 |
|
foreach ($urls as $url) { |
138
|
2 |
|
$ch = curl_init($url); |
139
|
2 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
140
|
2 |
|
curl_multi_add_handle($mCurl, $ch); |
|
|
|
|
141
|
2 |
|
$handles[$url] = $ch; |
142
|
|
|
} |
143
|
2 |
|
$this->startMultiCurl($mCurl); |
144
|
2 |
|
foreach ($handles as $channel) { |
145
|
2 |
|
$html = curl_multi_getcontent($channel); |
146
|
2 |
|
$json[] = json_decode($html, true); |
147
|
2 |
|
curl_multi_remove_handle($mCurl, $channel); |
148
|
|
|
} |
149
|
2 |
|
curl_multi_close($mCurl); |
150
|
2 |
|
return $json; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/* |
154
|
|
|
* Method for getting all urls for the multi curl method |
155
|
|
|
*/ |
156
|
2 |
|
private function getUrls($coords, $days) : array |
157
|
|
|
{ |
158
|
2 |
|
$urls = []; |
159
|
2 |
|
$location = $coords["lat"] . ',' . $coords["long"]; |
160
|
2 |
|
for ($i = 0; $i < $days; $i++) { |
161
|
2 |
|
$time = $this->getDayFormat("$i"); |
162
|
2 |
|
$urls[] = 'https://api.darksky.net/forecast/'.$this->apikey2.'/'.$location.','.$time.'?exclude=minutely,hourly,currently,alerts,flags&extend=daily&lang=sv&units=auto'; |
163
|
|
|
} |
164
|
2 |
|
return $urls; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/* |
168
|
|
|
* Method for converting time/date to correct format for darksky api |
169
|
|
|
*/ |
170
|
2 |
|
public function getDayFormat($day) |
171
|
|
|
{ |
172
|
2 |
|
$date = new Datetime(); |
173
|
2 |
|
$date->sub(new DateInterval('P'. (intval($day) + 1) .'D')); |
174
|
2 |
|
return $date->format('U'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/* |
178
|
|
|
* Method for actually fetching the multi curls |
179
|
|
|
*/ |
180
|
2 |
|
public function startMultiCurl($mCurl) |
181
|
|
|
{ |
182
|
|
|
do { |
183
|
2 |
|
$mrc = curl_multi_exec($mCurl, $active); |
184
|
2 |
|
} while ($mrc == CURLM_CALL_MULTI_PERFORM); |
185
|
2 |
|
while ($active && $mrc == CURLM_OK) { |
186
|
2 |
|
if (curl_multi_select($mCurl) == -1) { |
187
|
|
|
usleep(100); |
188
|
|
|
} |
189
|
|
|
do { |
190
|
2 |
|
$mrc = curl_multi_exec($mCurl, $active); |
191
|
2 |
|
} while ($mrc == CURLM_CALL_MULTI_PERFORM); |
192
|
|
|
} |
193
|
2 |
|
} |
194
|
|
|
} |
195
|
|
|
|