|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Moody\weather_and_position; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
|
|
8
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
|
9
|
|
|
// use Anax\Route\Exception\NotFoundException; |
|
10
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A sample controller to show how a controller class can be implemented. |
|
14
|
|
|
* The controller will be injected with $di if implementing the interface |
|
15
|
|
|
* ContainerInjectableInterface, like this sample class does. |
|
16
|
|
|
* The controller is mounted on a particular route and can then handle all |
|
17
|
|
|
* requests for that mount point. |
|
18
|
|
|
* |
|
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class IpValidate implements ContainerInjectableInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use ContainerInjectableTrait; |
|
28
|
|
|
protected $weatherAPI_next = ""; |
|
29
|
|
|
protected $weatherApiArr = []; |
|
30
|
|
|
public $message= "193.11.187.229"; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Sets the api from the config file |
|
35
|
|
|
* into the controller. |
|
36
|
|
|
* |
|
37
|
|
|
*/ |
|
38
|
22 |
|
public function setMessage($message) : void |
|
39
|
|
|
{ |
|
40
|
22 |
|
$this->message = $message; |
|
41
|
22 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* returns the api from the config file "ipstackcfg" |
|
49
|
|
|
* |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function getDetails() : string |
|
52
|
|
|
{ |
|
53
|
2 |
|
return $this->message; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Check if IP is valid or not. |
|
64
|
|
|
* GET ip |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
|
|
69
|
7 |
|
public function getProtocol($ipAddress) |
|
70
|
|
|
{ |
|
71
|
7 |
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP)) { |
|
72
|
4 |
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
73
|
3 |
|
return "IPv4"; |
|
74
|
|
|
} |
|
75
|
1 |
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
76
|
1 |
|
return "IPv6"; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
3 |
|
return false; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
/** |
|
82
|
|
|
* Check if IP is valid or not. |
|
83
|
|
|
* GET domain |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function getDomain($ipAddress) |
|
88
|
|
|
{ |
|
89
|
4 |
|
if (filter_var($ipAddress, FILTER_VALIDATE_IP)) { |
|
90
|
1 |
|
return gethostbyaddr($ipAddress); |
|
91
|
|
|
} |
|
92
|
3 |
|
return "Not valid"; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
3 |
|
public function getCurrentIp() |
|
97
|
|
|
{ |
|
98
|
3 |
|
return $_SERVER["193.11.187.229"] ?? '193.11.187.229'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
3 |
|
public function getAddress($ipAddress) |
|
104
|
|
|
{ |
|
105
|
3 |
|
$the_access_key = "9ffbd83fca588b355bff399b8da7526f"; |
|
106
|
3 |
|
$theUrl = "http://api.ipstack.com/"; |
|
107
|
3 |
|
$req = curl_init($theUrl . $ipAddress . "?access_key=" . $the_access_key . ''); |
|
108
|
3 |
|
curl_setopt($req, CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
|
|
109
|
3 |
|
$json = curl_exec($req); |
|
|
|
|
|
|
110
|
3 |
|
curl_close($req); |
|
|
|
|
|
|
111
|
3 |
|
$result = json_decode($json, 'JAON_PRETTY_PRINT'); |
|
|
|
|
|
|
112
|
3 |
|
return $result; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
public function setWeather($latitude, $longitude) : void |
|
116
|
|
|
{ |
|
117
|
1 |
|
$options = '&exclude=current,minutely,hourly,alerts&units=metric&lang=en'; |
|
118
|
1 |
|
$baseUrl = 'https://api.openweathermap.org/data/2.5/onecall?'; |
|
119
|
1 |
|
$the_access_key = "7baa582f6085fc9b4bcfbaceac7ff994"; |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
1 |
|
$this->weatherAPI_next = $baseUrl . 'lat=' . $latitude . '&lon=' . |
|
122
|
1 |
|
$longitude . $options . '&appid=' . $this->message; |
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
2 |
|
public function getWeather() |
|
126
|
|
|
{ |
|
127
|
2 |
|
return $this->weatherAPI_next; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Gets the Geo Location info in |
|
135
|
|
|
* Gets the Geo Location info in |
|
136
|
|
|
* an array |
|
137
|
|
|
* |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function getData(String $weatherApi) |
|
140
|
|
|
{ |
|
141
|
|
|
// create & initialize a curl session |
|
142
|
2 |
|
$curl = curl_init($weatherApi); |
|
143
|
|
|
|
|
144
|
|
|
// set our url with curl_setopt() |
|
145
|
2 |
|
curl_setopt($curl, CURLOPT_URL, $weatherApi); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
// return the transfer as a string, also with setopt() |
|
148
|
2 |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|
149
|
|
|
|
|
150
|
|
|
// curl_exec() executes the started curl session |
|
151
|
|
|
// $output contains the output string |
|
152
|
2 |
|
$output = curl_exec($curl); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
// close curl resource to free up system resources |
|
155
|
|
|
// (deletes the variable made by curl_init) |
|
156
|
2 |
|
curl_close($curl); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
2 |
|
$apiResult = json_decode($output, true); |
|
159
|
2 |
|
return $apiResult; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Sets the api from the config file |
|
168
|
|
|
* into the controller. |
|
169
|
|
|
* |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function setWeather2($latitude, $longitude) : void |
|
172
|
|
|
{ |
|
173
|
1 |
|
$the_access_key = "7baa582f6085fc9b4bcfbaceac7ff994"; |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
1 |
|
$options = '&exclude=minutely,hourly,alerts&units=metric&lang=en'; |
|
177
|
1 |
|
$baseUrl = 'http://api.openweathermap.org/data/2.5/onecall/timemachine?'; |
|
178
|
1 |
|
for ($i = -5; $i <= -1; $i++) { |
|
179
|
1 |
|
$datePrevious = strtotime("$i days"); |
|
180
|
1 |
|
array_push($this->weatherApiArr, $baseUrl . 'lat=' . $latitude . '&lon=' . |
|
181
|
1 |
|
$longitude . '&dt=' . $datePrevious . $options . '&appid=' . $this->message); |
|
182
|
|
|
} |
|
183
|
1 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
2 |
|
public function getWeather2() |
|
187
|
|
|
{ |
|
188
|
2 |
|
return $this->weatherApiArr; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
2 |
|
public function getDataArray(array $urls) |
|
192
|
|
|
{ |
|
193
|
|
|
|
|
194
|
|
|
$options = [ |
|
195
|
2 |
|
CURLOPT_RETURNTRANSFER => true, |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
// Add all curl handlers and remember them |
|
199
|
|
|
// Initiate the multi curl handler |
|
200
|
2 |
|
$multiCurl = curl_multi_init(); |
|
201
|
2 |
|
$chAll = []; |
|
202
|
2 |
|
foreach ($urls as $url) { |
|
203
|
2 |
|
$ch = curl_init("$url"); |
|
204
|
2 |
|
curl_setopt_array($ch, $options); |
|
|
|
|
|
|
205
|
2 |
|
curl_multi_add_handle($multiCurl, $ch); |
|
|
|
|
|
|
206
|
2 |
|
$chAll[] = $ch; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// Execute all queries simultaneously, |
|
210
|
|
|
// and continue when all are complete |
|
211
|
2 |
|
$running = null; |
|
212
|
|
|
do { |
|
213
|
2 |
|
curl_multi_exec($multiCurl, $running); |
|
214
|
2 |
|
} while ($running); |
|
215
|
|
|
|
|
216
|
|
|
// Close the handles |
|
217
|
2 |
|
foreach ($chAll as $ch) { |
|
218
|
2 |
|
curl_multi_remove_handle($multiCurl, $ch); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
2 |
|
curl_multi_close($multiCurl); |
|
223
|
|
|
|
|
224
|
|
|
// All of our requests are done, we can now access the results |
|
225
|
2 |
|
$response = []; |
|
226
|
2 |
|
foreach ($chAll as $ch) { |
|
227
|
2 |
|
$data = curl_multi_getcontent($ch); |
|
228
|
2 |
|
$response[] = json_decode($data, true); |
|
229
|
|
|
} |
|
230
|
2 |
|
return $response; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|