1 | <?php |
||||
2 | |||||
3 | namespace Anax\Models; |
||||
4 | |||||
5 | class Curl |
||||
6 | { |
||||
7 | 18 | public function curl($url) |
|||
8 | { |
||||
9 | // Initialize CURL: |
||||
10 | 18 | $curlHandle = curl_init($url); |
|||
11 | 18 | curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
12 | |||||
13 | // Store the data: |
||||
14 | 18 | $json = curl_exec($curlHandle); |
|||
0 ignored issues
–
show
It seems like
$curlHandle 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
![]() |
|||||
15 | 18 | curl_close($curlHandle); |
|||
0 ignored issues
–
show
It seems like
$curlHandle 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
![]() |
|||||
16 | |||||
17 | 18 | return $json; |
|||
18 | } |
||||
19 | |||||
20 | /** |
||||
21 | * |
||||
22 | * @SuppressWarnings(PHPMD.ShortVariable) |
||||
23 | */ |
||||
24 | 9 | public function multiCurl($urls) |
|||
25 | { |
||||
26 | 9 | $cHandles = []; |
|||
27 | |||||
28 | //create the multiple cURL handle |
||||
29 | 9 | $mh = curl_multi_init(); |
|||
30 | |||||
31 | 9 | $count = count($urls); |
|||
32 | |||||
33 | // set URL and other appropriate options |
||||
34 | 9 | for ($i = 0; $i < $count; $i++) { |
|||
35 | 9 | $cHandles[$i] = curl_init(); |
|||
36 | 9 | curl_setopt($cHandles[$i], CURLOPT_RETURNTRANSFER, true); |
|||
37 | 9 | curl_setopt($cHandles[$i], CURLOPT_URL, $urls[$i]); |
|||
38 | 9 | curl_setopt($cHandles[$i], CURLOPT_HEADER, 0); |
|||
39 | |||||
40 | 9 | curl_multi_add_handle($mh, $cHandles[$i]); |
|||
41 | } |
||||
42 | |||||
43 | //execute the multi handle |
||||
44 | do { |
||||
45 | 9 | $status = curl_multi_exec($mh, $active); |
|||
46 | 9 | if ($active) { |
|||
47 | 9 | curl_multi_select($mh); |
|||
48 | } |
||||
49 | 9 | } while ($active && $status == CURLM_OK); |
|||
50 | |||||
51 | 9 | $json = []; |
|||
52 | 9 | $countCHandles = count($cHandles); |
|||
53 | 9 | for ($i=0; $i < $countCHandles; $i++) { |
|||
54 | 9 | array_push($json, curl_multi_getcontent($cHandles[$i])); |
|||
55 | } |
||||
56 | |||||
57 | 9 | for ($i = 0; $i < $count; $i++) { |
|||
58 | 9 | curl_multi_remove_handle($mh, $cHandles[$i]); |
|||
59 | } |
||||
60 | |||||
61 | 9 | curl_multi_close($mh); |
|||
62 | |||||
63 | 9 | return $json; |
|||
64 | } |
||||
65 | } |
||||
66 |