jespernyhlen /
weather-module
| 1 | <?php |
||||
| 2 | namespace Anax\Curl; |
||||
| 3 | |||||
| 4 | /** |
||||
| 5 | * A model class retrievieng data from an external server. |
||||
| 6 | */ |
||||
| 7 | class CurlModel |
||||
| 8 | { |
||||
| 9 | |||||
| 10 | /** |
||||
| 11 | * Get curl from given url |
||||
| 12 | * |
||||
| 13 | * @return array |
||||
| 14 | */ |
||||
| 15 | 6 | public function getCurl(string $url) : array |
|||
| 16 | { |
||||
| 17 | // Initiate curl handler |
||||
| 18 | 6 | $ch = curl_init(); |
|||
| 19 | // Will return the response, if false it print the response |
||||
| 20 | 6 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 21 | // Set the url |
||||
| 22 | 6 | curl_setopt($ch, CURLOPT_URL, $url); |
|||
| 23 | // Execute |
||||
| 24 | 6 | $data = curl_exec($ch); |
|||
|
0 ignored issues
–
show
It seems like
$ch 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
Loading history...
|
|||||
| 25 | // Closing |
||||
| 26 | 6 | curl_close($ch); |
|||
|
0 ignored issues
–
show
It seems like
$ch 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
Loading history...
|
|||||
| 27 | |||||
| 28 | 6 | return json_decode($data, true) ?? []; |
|||
| 29 | } |
||||
| 30 | |||||
| 31 | |||||
| 32 | /** |
||||
| 33 | * Get curl using multicurl |
||||
| 34 | * |
||||
| 35 | * @return array |
||||
| 36 | */ |
||||
| 37 | 1 | public function getMultiCurl(array $urls) : array |
|||
| 38 | { |
||||
| 39 | $options = [ |
||||
| 40 | 1 | CURLOPT_RETURNTRANSFER => true, |
|||
| 41 | ]; |
||||
| 42 | // Add all curl handlers and remember them |
||||
| 43 | // Initiate the multi curl handler |
||||
| 44 | 1 | $mh = curl_multi_init(); |
|||
| 45 | 1 | $chAll = []; |
|||
| 46 | 1 | foreach ($urls as $url) { |
|||
| 47 | 1 | $ch = curl_init("$url"); |
|||
| 48 | 1 | 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
Loading history...
|
|||||
| 49 | 1 | curl_multi_add_handle($mh, $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
Loading history...
|
|||||
| 50 | 1 | $chAll[] = $ch; |
|||
| 51 | } |
||||
| 52 | // Execute all queries simultaneously, |
||||
| 53 | // and continue when all are complete |
||||
| 54 | 1 | $running = null; |
|||
| 55 | do { |
||||
| 56 | 1 | curl_multi_exec($mh, $running); |
|||
| 57 | 1 | } while ($running); |
|||
| 58 | // Close the handles |
||||
| 59 | 1 | foreach ($chAll as $ch) { |
|||
| 60 | 1 | curl_multi_remove_handle($mh, $ch); |
|||
| 61 | } |
||||
| 62 | 1 | curl_multi_close($mh); |
|||
| 63 | // All of our requests are done, we can now access the results |
||||
| 64 | 1 | $response = []; |
|||
| 65 | 1 | foreach ($chAll as $ch) { |
|||
| 66 | 1 | $data = curl_multi_getcontent($ch); |
|||
| 67 | 1 | $response[] = json_decode($data, true); |
|||
| 68 | } |
||||
| 69 | 1 | return $response; |
|||
| 70 | } |
||||
| 71 | } |
||||
| 72 |