1 | <?php |
||||||
2 | |||||||
3 | namespace Faxity\Fetch; |
||||||
4 | |||||||
5 | /** |
||||||
6 | * Send HTTP requests via cURL |
||||||
7 | */ |
||||||
8 | class Fetch |
||||||
9 | { |
||||||
10 | /** |
||||||
11 | * Initializes a curl request |
||||||
12 | * @param string $url URL to send request to |
||||||
13 | * @param mixed $params Optional query parameters as an array or object |
||||||
14 | * |
||||||
15 | * @return resource|bool |
||||||
16 | */ |
||||||
17 | 10 | private function initRequest(string $url, $params) |
|||||
18 | { |
||||||
19 | 10 | if ($params) { |
|||||
20 | 8 | $query = ""; |
|||||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||||||
21 | |||||||
22 | 8 | if (is_object($params) || is_array($params)) { |
|||||
23 | 8 | $query = http_build_query($params); |
|||||
24 | 8 | $url .= $query ? "?$query" : ""; |
|||||
25 | } |
||||||
26 | } |
||||||
27 | |||||||
28 | 10 | $curl = curl_init($url); |
|||||
29 | 10 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|||||
0 ignored issues
–
show
It seems like
$curl 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
![]() |
|||||||
30 | |||||||
31 | 10 | return $curl; |
|||||
32 | } |
||||||
33 | |||||||
34 | |||||||
35 | /** |
||||||
36 | * Sends a GET request to URL |
||||||
37 | * @param string $url URL to send request to |
||||||
38 | * @param mixed $params Optional query parameters as an array or object |
||||||
39 | * |
||||||
40 | * @return object |
||||||
41 | */ |
||||||
42 | 7 | public function get(string $url, $params = null) : ?object |
|||||
43 | { |
||||||
44 | 7 | $curl = self::initRequest($url, $params); |
|||||
0 ignored issues
–
show
The method
Faxity\Fetch\Fetch::initRequest() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
45 | |||||||
46 | 7 | $body = curl_exec($curl); |
|||||
0 ignored issues
–
show
It seems like
$curl 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
![]() |
|||||||
47 | 7 | curl_close($curl); |
|||||
0 ignored issues
–
show
It seems like
$curl 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
![]() |
|||||||
48 | |||||||
49 | // Parse response as json |
||||||
50 | 7 | return json_decode($body); |
|||||
51 | } |
||||||
52 | |||||||
53 | |||||||
54 | /** |
||||||
55 | * Sends multiple GET requests in parallell |
||||||
56 | * @param array $requests An array of assoc-arrays with url and params as keys, like: |
||||||
57 | * [ |
||||||
58 | * "url" => "", |
||||||
59 | * "params" => [ "param" => "" ] |
||||||
60 | * ] |
||||||
61 | */ |
||||||
62 | 3 | public function getMulti(array $requests) |
|||||
63 | { |
||||||
64 | 3 | $multi = curl_multi_init(); |
|||||
65 | |||||||
66 | // Create handles for cURL request |
||||||
67 | $handles = array_map(function ($item) use ($multi) { |
||||||
68 | 3 | $handle = self::initRequest($item["url"], $item["params"] ?? null); |
|||||
0 ignored issues
–
show
The method
Faxity\Fetch\Fetch::initRequest() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
69 | 3 | curl_multi_add_handle($multi, $handle); |
|||||
0 ignored issues
–
show
It seems like
$handle 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
![]() |
|||||||
70 | |||||||
71 | 3 | return $handle; |
|||||
72 | 3 | }, $requests); |
|||||
73 | |||||||
74 | // Execute requests in parallell and wait for all to complete |
||||||
75 | do { |
||||||
76 | 3 | $status = curl_multi_exec($multi, $active); |
|||||
77 | 3 | $active && curl_multi_select($multi); // Prevents CPU overusage |
|||||
78 | 3 | } while ($active && $status == CURLM_OK); |
|||||
79 | |||||||
80 | |||||||
81 | // Cleanup and get content |
||||||
82 | $content = array_map(function ($handle) use ($multi) { |
||||||
83 | 3 | curl_multi_remove_handle($multi, $handle); |
|||||
84 | |||||||
85 | 3 | $body = curl_multi_getcontent($handle); |
|||||
86 | 3 | return json_decode($body); |
|||||
87 | 3 | }, $handles); |
|||||
88 | |||||||
89 | 3 | curl_multi_close($multi); |
|||||
90 | |||||||
91 | 3 | return $content; |
|||||
92 | } |
||||||
93 | } |
||||||
94 |