1 | <?php |
||||
2 | |||||
3 | namespace Bashar\WeatherModel; |
||||
4 | |||||
5 | /** |
||||
6 | * A sample controller to show how a controller class can be implemented. |
||||
7 | * The controller will be injected with $di if implementing the interface |
||||
8 | * ContainerInjectableInterface, like this sample class does. |
||||
9 | * The controller is mounted on a particular route and can then handle all |
||||
10 | * requests for that mount point. |
||||
11 | * |
||||
12 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||||
13 | */ |
||||
14 | class Curl |
||||
15 | { |
||||
16 | /** |
||||
17 | * Gets the Geo Location info in |
||||
18 | * Gets the Geo Location info in |
||||
19 | * an array |
||||
20 | * |
||||
21 | */ |
||||
22 | 4 | public function getData(String $weatherApi) |
|||
23 | { |
||||
24 | // create & initialize a curl session |
||||
25 | 4 | $curl = curl_init($weatherApi); |
|||
26 | |||||
27 | // set our url with curl_setopt() |
||||
28 | 4 | curl_setopt($curl, CURLOPT_URL, $weatherApi); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
29 | |||||
30 | // return the transfer as a string, also with setopt() |
||||
31 | 4 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|||
32 | |||||
33 | // curl_exec() executes the started curl session |
||||
34 | // $output contains the output string |
||||
35 | 4 | $output = 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
![]() |
|||||
36 | |||||
37 | // close curl resource to free up system resources |
||||
38 | // (deletes the variable made by curl_init) |
||||
39 | 4 | 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
![]() |
|||||
40 | |||||
41 | 4 | $apiResult = json_decode($output, true); |
|||
42 | 4 | return $apiResult; |
|||
43 | } |
||||
44 | |||||
45 | |||||
46 | /** |
||||
47 | * Function that uses multiple curls and returns response |
||||
48 | * |
||||
49 | * @param array $urls API URLs to use in curls |
||||
50 | * |
||||
51 | * @return array $res Result in JSON |
||||
52 | */ |
||||
53 | 2 | public function getDataArray(Array $urls) |
|||
54 | { |
||||
55 | $options = [ |
||||
56 | 2 | CURLOPT_RETURNTRANSFER => true, |
|||
57 | ]; |
||||
58 | |||||
59 | // Add all curl handlers and remember them |
||||
60 | // Initiate the multi curl handler |
||||
61 | 2 | $multiCurl = curl_multi_init(); |
|||
62 | 2 | $chAll = []; |
|||
63 | 2 | foreach ($urls as $url) { |
|||
64 | 2 | $ch = curl_init("$url"); |
|||
65 | 2 | 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
![]() |
|||||
66 | 2 | curl_multi_add_handle($multiCurl, $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
![]() |
|||||
67 | 2 | $chAll[] = $ch; |
|||
68 | } |
||||
69 | |||||
70 | // Execute all queries simultaneously, |
||||
71 | // and continue when all are complete |
||||
72 | 2 | $running = null; |
|||
73 | do { |
||||
74 | 2 | curl_multi_exec($multiCurl, $running); |
|||
75 | 2 | } while ($running); |
|||
76 | |||||
77 | // Close the handles |
||||
78 | 2 | foreach ($chAll as $ch) { |
|||
79 | 2 | curl_multi_remove_handle($multiCurl, $ch); |
|||
80 | } |
||||
81 | 2 | curl_multi_close($multiCurl); |
|||
82 | |||||
83 | // All of our requests are done, we can now access the results |
||||
84 | 2 | $response = []; |
|||
85 | 2 | foreach ($chAll as $ch) { |
|||
86 | 2 | $data = curl_multi_getcontent($ch); |
|||
87 | 2 | $response[] = json_decode($data, true); |
|||
88 | } |
||||
89 | 2 | return $response; |
|||
90 | } |
||||
91 | } |
||||
92 |