Conditions | 4 |
Paths | 4 |
Total Lines | 36 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Tests | 18 |
CRAP Score | 4 |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
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); |
|
50 | 1 | curl_multi_add_handle($mh, $ch); |
|
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); |
|
68 | 1 | curl_multi_close($mh); |
|
69 | |||
70 | 1 | return $response; |
|
71 | } |
||
73 |