AlexanderGranhof /
weather
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace algn\DarkSky; |
||||
| 4 | |||||
| 5 | class DarkSky |
||||
| 6 | { |
||||
| 7 | public function __construct($key) |
||||
| 8 | { |
||||
| 9 | $this->key = $key; |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 10 | $this->url = "https://api.darksky.net/forecast/"; |
||||
|
0 ignored issues
–
show
|
|||||
| 11 | $this->exclude = "?exclude=minutely,hourly,alerts,flags,daily&units=si"; |
||||
|
0 ignored issues
–
show
|
|||||
| 12 | } |
||||
| 13 | |||||
| 14 | public function week($lat, $lng): object |
||||
| 15 | { |
||||
| 16 | $response = file_get_contents($this->url . $this->key . "/" . strval($lat) . "," . strval($lng) . "?exclude=minutely,hourly,alerts,flags,currently"); |
||||
| 17 | |||||
| 18 | return json_decode($response); |
||||
| 19 | } |
||||
| 20 | |||||
| 21 | public function today($lat, $lng, $time = null) |
||||
| 22 | { |
||||
| 23 | $today = date("Y-m-d") . "T" . date("H:i:s"); |
||||
| 24 | |||||
| 25 | if ($time) { |
||||
| 26 | $today = $time; |
||||
| 27 | } |
||||
| 28 | |||||
| 29 | |||||
| 30 | $excludes = $this->exclude; |
||||
| 31 | $response = file_get_contents($this->url . $this->key . "/" . strval($lat) . "," . strval($lng) . "," . $today . $excludes); |
||||
| 32 | |||||
| 33 | return json_decode($response); |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | public function past30Days($lat, $lng) |
||||
| 37 | { |
||||
| 38 | $chAll = []; |
||||
| 39 | $mhs = curl_multi_init(); |
||||
| 40 | |||||
| 41 | $position = strval($lat) . "," . strval($lng); |
||||
| 42 | |||||
| 43 | for ($i = 0; $i < 30; $i++) { |
||||
| 44 | $offset = $i + 1; |
||||
| 45 | $ourTime = date("Y-m-d", strtotime("-$offset days")) . "T" . date("H:i:s"); |
||||
| 46 | |||||
| 47 | $exclude = $this->exclude; |
||||
| 48 | $key = $this->key; |
||||
| 49 | |||||
| 50 | $url = "https://api.darksky.net/forecast/$key/$position,$ourTime$exclude"; |
||||
| 51 | |||||
| 52 | $chs = curl_init($url); |
||||
| 53 | curl_setopt_array($chs, [CURLOPT_RETURNTRANSFER => true]); |
||||
|
0 ignored issues
–
show
It seems like
$chs 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...
|
|||||
| 54 | curl_multi_add_handle($mhs, $chs); |
||||
|
0 ignored issues
–
show
It seems like
$chs 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...
|
|||||
| 55 | |||||
| 56 | $chAll[] = $chs; |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | $running = null; |
||||
| 60 | |||||
| 61 | do { |
||||
| 62 | curl_multi_exec($mhs, $running); |
||||
| 63 | } while ($running); |
||||
| 64 | |||||
| 65 | foreach ($chAll as $chs) { |
||||
| 66 | curl_multi_remove_handle($mhs, $chs); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | curl_multi_close($mhs); |
||||
| 70 | |||||
| 71 | $response = []; |
||||
| 72 | |||||
| 73 | foreach ($chAll as $chs) { |
||||
| 74 | $data = curl_multi_getcontent($chs); |
||||
| 75 | $response[] = json_decode($data, true); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | return json_encode($response); |
||||
| 79 | } |
||||
| 80 | } |
||||
| 81 |