ejessyp /
weather
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Anax\OpenWeather; |
||||
| 4 | |||||
| 5 | |||||
| 6 | /** |
||||
| 7 | * A model class retrievieng data from an external server. |
||||
| 8 | * |
||||
| 9 | * @SuppressWarnings(PHPMD.ShortVariable) |
||||
| 10 | */ |
||||
| 11 | class OpenWeather |
||||
| 12 | { |
||||
| 13 | |||||
| 14 | // load the config file of key |
||||
| 15 | private $apikey = ""; |
||||
| 16 | |||||
| 17 | 2 | public function setKey($key) : void |
|||
| 18 | { |
||||
| 19 | 2 | $this->apikey = $key; |
|||
| 20 | 2 | } |
|||
| 21 | |||||
| 22 | 2 | public function getCurl($lat, $lon) : array |
|||
| 23 | { |
||||
| 24 | 2 | $url = "https://api.openweathermap.org/data/2.5/onecall?lat=$lat&lon=$lon&units=metric&appid={$this->apikey}"; |
|||
| 25 | |||||
| 26 | // Initiate curl handler |
||||
| 27 | 2 | $ch = curl_init(); |
|||
| 28 | |||||
| 29 | // Will return the response, if false it print the response |
||||
| 30 | 2 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 31 | |||||
| 32 | // Set the url |
||||
| 33 | 2 | curl_setopt($ch, CURLOPT_URL, $url); |
|||
| 34 | |||||
| 35 | // Execute |
||||
| 36 | 2 | $data = curl_exec($ch); |
|||
|
0 ignored issues
–
show
It seems like
$ch 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
Loading history...
|
|||||
| 37 | |||||
| 38 | // Closing |
||||
| 39 | 2 | curl_close($ch); |
|||
|
0 ignored issues
–
show
It seems like
$ch 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
Loading history...
|
|||||
| 40 | // var_dump($data); |
||||
| 41 | 2 | $weather = json_decode($data, true); |
|||
| 42 | 2 | return $weather; |
|||
| 43 | } |
||||
| 44 | |||||
| 45 | |||||
| 46 | 2 | public function getHistoryMCurl($lat, $lon, $fiveDays) : array |
|||
| 47 | { |
||||
| 48 | 2 | $url = "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&units=metric&appid={$this->apikey}"; |
|||
| 49 | // var_dump($fiveDays); |
||||
| 50 | $options = [ |
||||
| 51 | 2 | CURLOPT_RETURNTRANSFER => true, |
|||
| 52 | ]; |
||||
| 53 | |||||
| 54 | // Add all curl handlers and remember them |
||||
| 55 | // Initiate the multi curl handler |
||||
| 56 | 2 | $mh = curl_multi_init(); |
|||
| 57 | 2 | $chAll = []; |
|||
| 58 | 2 | foreach ($fiveDays as $dt) { |
|||
| 59 | 2 | $ch = curl_init("$url&dt=$dt"); |
|||
| 60 | 2 | curl_setopt_array($ch, $options); |
|||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_setopt_array() 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
Loading history...
|
|||||
| 61 | 2 | curl_multi_add_handle($mh, $ch); |
|||
|
0 ignored issues
–
show
It seems like
$ch 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
Loading history...
|
|||||
| 62 | 2 | $chAll[] = $ch; |
|||
| 63 | } |
||||
| 64 | |||||
| 65 | // Execute all queries simultaneously, |
||||
| 66 | // and continue when all are complete |
||||
| 67 | 2 | $running = null; |
|||
| 68 | do { |
||||
| 69 | 2 | curl_multi_exec($mh, $running); |
|||
| 70 | 2 | } while ($running); |
|||
| 71 | |||||
| 72 | // Close the handles |
||||
| 73 | 2 | foreach ($chAll as $ch) { |
|||
| 74 | 2 | curl_multi_remove_handle($mh, $ch); |
|||
| 75 | } |
||||
| 76 | 2 | curl_multi_close($mh); |
|||
| 77 | |||||
| 78 | // All of our requests are done, we can now access the results |
||||
| 79 | 2 | $response = []; |
|||
| 80 | 2 | foreach ($chAll as $ch) { |
|||
| 81 | |||||
| 82 | 2 | $data = curl_multi_getcontent($ch); |
|||
| 83 | // var_dump(json_decode($data, true)); |
||||
| 84 | |||||
| 85 | 2 | $data = json_decode($data, true)['current']; |
|||
| 86 | 2 | $response[] = $data; |
|||
| 87 | } |
||||
| 88 | |||||
| 89 | 2 | return $response; |
|||
| 90 | } |
||||
| 91 | } |
||||
| 92 |