heidipatja /
ramverk1-weather-module
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * Curl model |
||||
| 5 | */ |
||||
| 6 | |||||
| 7 | namespace Hepa19\Curl; |
||||
| 8 | |||||
| 9 | /** |
||||
| 10 | * Get data via public API |
||||
| 11 | * |
||||
| 12 | */ |
||||
| 13 | class Curl |
||||
| 14 | { |
||||
| 15 | /** |
||||
| 16 | * Function that uses curl and returns response |
||||
| 17 | * |
||||
| 18 | * @param string $url API URL to use in curl |
||||
| 19 | * |
||||
| 20 | * @return array $res Result in JSON |
||||
| 21 | */ |
||||
| 22 | |||||
| 23 | public function getData(String $url) |
||||
| 24 | { |
||||
| 25 | $ch = curl_init($url); |
||||
| 26 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 27 | $res = 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...
|
|||||
| 28 | 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...
|
|||||
| 29 | $res = json_decode($res, true); |
||||
| 30 | |||||
| 31 | return $res; |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | |||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * Function that uses multiple curls and returns response |
||||
| 38 | * |
||||
| 39 | * @param array $urls API URLs to use in curls |
||||
| 40 | * |
||||
| 41 | * @return array $res Result in JSON |
||||
| 42 | */ |
||||
| 43 | public function getMultiData(Array $urls) |
||||
| 44 | { |
||||
| 45 | $mh = curl_multi_init(); |
||||
| 46 | |||||
| 47 | $chArray = []; |
||||
| 48 | |||||
| 49 | foreach ($urls as $url) { |
||||
| 50 | $ch = curl_init($url); |
||||
| 51 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||||
|
0 ignored issues
–
show
It seems like
$ch 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
Loading history...
|
|||||
| 52 | 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...
|
|||||
| 53 | array_push($chArray, $ch); |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | do { |
||||
| 57 | $status = curl_multi_exec($mh, $active); |
||||
| 58 | if ($active) { |
||||
| 59 | curl_multi_select($mh); |
||||
| 60 | } |
||||
| 61 | } while ($active && $status == CURLM_OK); |
||||
| 62 | |||||
| 63 | $res = []; |
||||
| 64 | |||||
| 65 | foreach ($chArray as $ch) { |
||||
| 66 | $response = curl_multi_getcontent($ch); |
||||
| 67 | $res[] = json_decode($response, true); |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | return $res; |
||||
| 71 | } |
||||
| 72 | } |
||||
| 73 |