1 | <?php |
||||
2 | namespace Aisa\WeatherRequest; |
||||
3 | |||||
4 | |||||
5 | use Anax\Commons\ContainerInjectableInterface; |
||||
6 | use Anax\Commons\ContainerInjectableTrait; |
||||
7 | // use Anax\Route\Exception\ForbiddenException; |
||||
8 | // use Anax\Route\Exception\NotFoundException; |
||||
9 | // use Anax\Route\Exception\InternalErrorException; |
||||
10 | /** |
||||
11 | * A sample controller to show how a controller class can be implemented. |
||||
12 | * The controller will be injected with $di if implementing the interface |
||||
13 | * ContainerInjectableInterface, like this sample class does. |
||||
14 | * The controller is mounted on a particular route and can then handle all |
||||
15 | * requests for that mount point. |
||||
16 | * |
||||
17 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||||
18 | */ |
||||
19 | class WeatherRequest implements ContainerInjectableInterface |
||||
20 | { |
||||
21 | use ContainerInjectableTrait; |
||||
22 | /** |
||||
23 | * Curl URL and return JSON |
||||
24 | * |
||||
25 | * @return array |
||||
26 | */ |
||||
27 | 3 | public function curlJson($url) |
|||
28 | { |
||||
29 | // Initialize CURL: |
||||
30 | 3 | $initization = curl_init($url); |
|||
31 | 3 | curl_setopt($initization, CURLOPT_RETURNTRANSFER, true); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
32 | // Store the data: |
||||
33 | 3 | $json = curl_exec($initization); |
|||
0 ignored issues
–
show
It seems like
$initization 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
![]() |
|||||
34 | 3 | curl_close($initization); |
|||
0 ignored issues
–
show
It seems like
$initization 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
![]() |
|||||
35 | // Decode JSON response: |
||||
36 | 3 | return json_decode($json, true); |
|||
37 | } |
||||
38 | |||||
39 | /** |
||||
40 | * Curl multi |
||||
41 | * |
||||
42 | * @return array |
||||
43 | */ |
||||
44 | 3 | public function multiRequest($data, $options = array()) |
|||
45 | { |
||||
46 | // array of curl handles |
||||
47 | 3 | $curly = array(); |
|||
48 | // data to be returned |
||||
49 | 3 | $result = array(); |
|||
50 | // multi handle |
||||
51 | 3 | $mh = curl_multi_init(); |
|||
52 | // loop through $data and create curl handles |
||||
53 | // then add them to the multi-handle |
||||
54 | 3 | foreach ($data as $id => $d) { |
|||
55 | 1 | $curly[$id] = curl_init(); |
|||
56 | 1 | $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; |
|||
57 | 1 | curl_setopt($curly[$id], CURLOPT_URL, $url); |
|||
58 | 1 | curl_setopt($curly[$id], CURLOPT_HEADER, 0); |
|||
59 | 1 | curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); |
|||
60 | // post? |
||||
61 | 1 | if (is_array($d)) { |
|||
62 | if (!empty($d['post'])) { |
||||
63 | curl_setopt($curly[$id], CURLOPT_POST, 1); |
||||
64 | curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); |
||||
65 | } |
||||
66 | } |
||||
67 | // extra options? |
||||
68 | 1 | if (!empty($options)) { |
|||
69 | curl_setopt_array($curly[$id], $options); |
||||
70 | } |
||||
71 | 1 | curl_multi_add_handle($mh, $curly[$id]); |
|||
72 | } |
||||
73 | // execute the handles |
||||
74 | 3 | $running = null; |
|||
75 | do { |
||||
76 | 3 | curl_multi_exec($mh, $running); |
|||
77 | 3 | } while ($running > 0); |
|||
78 | // get content and remove handles |
||||
79 | 3 | foreach ($curly as $id => $c) { |
|||
80 | 1 | $result[$id] = curl_multi_getcontent($c); |
|||
81 | 1 | curl_multi_remove_handle($mh, $c); |
|||
82 | } |
||||
83 | // all done |
||||
84 | 3 | curl_multi_close($mh); |
|||
85 | 3 | return $result; |
|||
86 | } |
||||
87 | } |
||||
88 |