1 | <?php |
||
2 | |||
3 | namespace Pamo\MultiCurl; |
||
4 | |||
5 | /** |
||
6 | * Multi curl |
||
7 | */ |
||
8 | class MultiCurl |
||
9 | { |
||
10 | /** |
||
11 | * Multi curl |
||
12 | * |
||
13 | * @param array $url list to curl. |
||
14 | * |
||
15 | * @return array |
||
16 | */ |
||
17 | 1 | public function get(array $urls, $header) : array |
|
18 | { |
||
19 | 1 | $id = 1; |
|
20 | 1 | $data = []; |
|
21 | 1 | $headers = $header; |
|
22 | |||
23 | //create the multiple cURL handle |
||
24 | 1 | $curlHandler = curl_multi_init(); |
|
25 | |||
26 | 1 | foreach ($urls as $url) { |
|
27 | // create cURL resources |
||
28 | 1 | ${"ch$id"} = curl_init($url); |
|
29 | |||
30 | // set URL and other appropriate options |
||
31 | 1 | curl_setopt(${"ch$id"}, CURLOPT_RETURNTRANSFER, true); |
|
32 | 1 | curl_setopt(${"ch$id"}, CURLOPT_HTTPHEADER, $headers); |
|
33 | |||
34 | //add the handle |
||
35 | 1 | curl_multi_add_handle($curlHandler, ${"ch$id"}); |
|
36 | |||
37 | 1 | $id++; |
|
38 | } |
||
39 | |||
40 | //execute the multi handle |
||
41 | 1 | $running = null; |
|
42 | do { |
||
43 | 1 | curl_multi_exec($curlHandler, $running); |
|
44 | 1 | } while ($running); |
|
45 | |||
46 | 1 | $id = 1; |
|
47 | |||
48 | 1 | foreach ($urls as $url) { |
|
49 | //close the handle |
||
50 | 1 | curl_multi_remove_handle($curlHandler, ${"ch$id"}); |
|
51 | 1 | $contents = json_decode(curl_multi_getcontent(${"ch$id"}), JSON_UNESCAPED_UNICODE |true); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
52 | 1 | $data[] = $contents; |
|
53 | 1 | $id++; |
|
54 | } |
||
55 | |||
56 | 1 | curl_multi_close($curlHandler); |
|
57 | |||
58 | 1 | return $data; |
|
59 | } |
||
60 | } |
||
61 |