jeneljenel /
weather-module
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Jenel\Curl; |
||||
| 4 | |||||
| 5 | /** |
||||
| 6 | * A class to place service in $di. |
||||
| 7 | * Curl |
||||
| 8 | * Return array |
||||
| 9 | */ |
||||
| 10 | class Curl |
||||
| 11 | { |
||||
| 12 | /** |
||||
| 13 | * Curl one. |
||||
| 14 | * @param url |
||||
| 15 | * |
||||
| 16 | * @return array |
||||
| 17 | */ |
||||
| 18 | 4 | public function curlOne($url) : array |
|||
| 19 | { |
||||
| 20 | 4 | $ch = curl_init($url); |
|||
| 21 | 4 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 22 | 4 | $response = 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...
|
|||||
| 23 | 4 | 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...
|
|||||
| 24 | |||||
| 25 | |||||
| 26 | 4 | return json_decode($response, true); |
|||
| 27 | } |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * Curl multi. |
||||
| 31 | * @param urls |
||||
| 32 | * |
||||
| 33 | * @return array |
||||
| 34 | */ |
||||
| 35 | 1 | public function curlMulti($urls) : array |
|||
| 36 | { |
||||
| 37 | $options = [ |
||||
| 38 | 1 | CURLOPT_RETURNTRANSFER => true, |
|||
| 39 | ]; |
||||
| 40 | |||||
| 41 | //set the multi handler |
||||
| 42 | 1 | $mh = curl_multi_init(); |
|||
| 43 | |||||
| 44 | //set variable to catch all |
||||
| 45 | 1 | $chAll = []; |
|||
| 46 | |||||
| 47 | 1 | foreach ($urls as $url) { |
|||
| 48 | 1 | $ch = curl_init($url); |
|||
| 49 | 1 | 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...
|
|||||
| 50 | 1 | 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...
|
|||||
| 51 | 1 | $chAll[] = $ch; |
|||
| 52 | }; |
||||
| 53 | |||||
| 54 | // execute all queries simultaneously, and continue when all are complete |
||||
| 55 | 1 | $running = null; |
|||
| 56 | do { |
||||
| 57 | 1 | curl_multi_exec($mh, $running); |
|||
| 58 | 1 | } while ($running); |
|||
| 59 | |||||
| 60 | //fix to return as array json |
||||
| 61 | 1 | foreach ($chAll as $ch) { |
|||
| 62 | 1 | $data = curl_multi_getcontent($ch); |
|||
| 63 | 1 | $response[] = json_decode($data, true); |
|||
| 64 | }; |
||||
| 65 | |||||
| 66 | //close the handles |
||||
| 67 | 1 | curl_multi_remove_handle($mh, $ch); |
|||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 68 | 1 | curl_multi_close($mh); |
|||
| 69 | |||||
| 70 | 1 | return $response; |
|||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 71 | } |
||||
| 72 | } |
||||
| 73 |