1 | <?php |
||||
2 | namespace Lioo19\Models; |
||||
3 | |||||
4 | /** |
||||
5 | * Class for retriving weather data |
||||
6 | * |
||||
7 | */ |
||||
8 | class Weather |
||||
9 | { |
||||
10 | private function getApikey() |
||||
11 | { |
||||
12 | $apikey = require ANAX_INSTALL_PATH . "/config/apikeys.php"; |
||||
13 | $apikey = $apikey["openweathermap"]; |
||||
14 | return $apikey; |
||||
15 | } |
||||
16 | |||||
17 | /** |
||||
18 | * Method for retriving the current date, and the five previous days |
||||
19 | * @return array with the different dates given in unix |
||||
20 | * |
||||
21 | */ |
||||
22 | 1 | public function getDate() |
|||
23 | { |
||||
24 | 1 | $days = []; |
|||
25 | 1 | for ($i = 0; $i > -5; $i--) { |
|||
26 | 1 | $days[] = strtotime("$i days"); |
|||
27 | } |
||||
28 | |||||
29 | 1 | return $days; |
|||
30 | } |
||||
31 | |||||
32 | /** |
||||
33 | * Method for retriving the weather info, given coordinates |
||||
34 | * |
||||
35 | */ |
||||
36 | public function fetchForecastWeather($lon, $lat, $url = "api.openweathermap.org/data/2.5/onecall?lat=") |
||||
37 | { |
||||
38 | $curl = curl_init(); |
||||
39 | $apikey = $this->getApikey(); |
||||
40 | if (strlen($lon) > 0 && strlen($lat) > 0) { |
||||
41 | //sets the url for curl to the correct one |
||||
42 | curl_setopt($curl, CURLOPT_URL, "$url" . $lat . "&lon=" . $lon . |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
43 | "&exclude=minutely,hourly&lang=se&units=metric&APPID=" . $apikey); |
||||
44 | //returns a string |
||||
45 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||||
46 | //execute the started curl-session |
||||
47 | $output = curl_exec($curl); |
||||
0 ignored issues
–
show
It seems like
$curl can also be of type false ; however, parameter $ch of curl_exec() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
48 | $exploded = json_decode($output, true); |
||||
49 | |||||
50 | // $data = $exploded; |
||||
51 | if (array_key_exists("message", $exploded)) { |
||||
52 | $data = [ |
||||
53 | "message" => $exploded["message"] |
||||
54 | ]; |
||||
55 | return $data; |
||||
56 | } |
||||
57 | $data = $exploded; |
||||
58 | //close curl-session to free up space |
||||
59 | curl_close($curl); |
||||
0 ignored issues
–
show
It seems like
$curl can also be of type false ; however, parameter $ch of curl_close() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
60 | |||||
61 | return $data; |
||||
62 | } else { |
||||
63 | return "Not a valid lon/lat"; |
||||
64 | } |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * Method for retriving the weather info, given coordinates |
||||
69 | * @return object With parts of valid JSON-repsonse |
||||
70 | * Need to make five different API-calls, one for each day |
||||
71 | * |
||||
72 | */ |
||||
73 | public function fetchHistoricalWeather($lon, $lat) |
||||
74 | { |
||||
75 | $apikey = $this->getApikey(); |
||||
76 | $url = "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=" |
||||
77 | . $lat . "&lon=" . $lon . "&lang=se&units=metric&dt="; |
||||
78 | $urlcont = "&APPID=" . $apikey; |
||||
79 | $days = $this->getDate(); |
||||
80 | |||||
81 | //sets the url for curl to the correct one |
||||
82 | $multi = curl_multi_init(); |
||||
83 | $all = []; |
||||
84 | foreach ($days as $day) { |
||||
85 | $c = curl_init($url . $day . $urlcont); |
||||
86 | curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); |
||||
0 ignored issues
–
show
It seems like
$c can also be of type false ; however, parameter $ch of curl_setopt() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
87 | curl_multi_add_handle($multi, $c); |
||||
0 ignored issues
–
show
It seems like
$c can also be of type false ; however, parameter $ch of curl_multi_add_handle() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
88 | $all[] = $c; |
||||
89 | } |
||||
90 | |||||
91 | $run = null; |
||||
92 | |||||
93 | do { |
||||
94 | curl_multi_exec($multi, $run); |
||||
95 | } while ($run); |
||||
96 | |||||
97 | //remove handles |
||||
98 | foreach ($all as $c) { |
||||
99 | curl_multi_remove_handle($multi, $c); |
||||
100 | } |
||||
101 | //close curl sessions |
||||
102 | curl_multi_close($multi); |
||||
103 | |||||
104 | $res = []; |
||||
105 | // $res = $days; |
||||
106 | |||||
107 | |||||
108 | foreach ($all as $c) { |
||||
109 | $output = curl_multi_getcontent($c); |
||||
110 | $exploded = json_decode($output, true); |
||||
111 | if (array_key_exists("message", $exploded)) { |
||||
112 | $data = [ |
||||
113 | "message" => $exploded["message"] |
||||
114 | ]; |
||||
115 | return $data; |
||||
0 ignored issues
–
show
|
|||||
116 | } else { |
||||
117 | $exploded = $exploded["current"]; |
||||
118 | } |
||||
119 | |||||
120 | $res[] = $exploded; |
||||
121 | } |
||||
122 | |||||
123 | return $res; |
||||
0 ignored issues
–
show
|
|||||
124 | } |
||||
125 | } |
||||
126 |