Issues (11)

src/Curl/Curl.php (5 issues)

Labels
Severity
1
<?php
2
3
namespace Bjos\Curl;
4
5
/**
6
 * A class that manages curl requests.
7
 * @SuppressWarnings(PHPMD.ShortVariable)
8
 */
9
class Curl
10
{
11
12
    /**
13
     * Curl method.
14
     *
15
     * @return array|void $res
16
     */
17 2
    public function curlApi(string $url = null)
18
    {
19 2
        $res = null;
20
21 2
        if ($url) {
22 1
            $curl = curl_init();
23 1
            curl_setopt($curl, CURLOPT_URL, $url);
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 ignore-type  annotation

23
            curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_URL, $url);
Loading history...
24 1
            curl_setopt($curl, CURLOPT_HEADER, 0);
25 1
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
26 1
            $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 ignore-type  annotation

26
            $output = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
27 1
            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 ignore-type  annotation

27
            curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
28 1
            $res = json_decode($output, true);
29
        }
30
31 2
        return $res;
32
    }
33
34
    /**
35
     * multicurl method
36
     *
37
     * @return array
38
     */
39 1
    public function multiCurlApi(array $urls) : array
40
    {
41 1
        $response = [];
42
43 1
        if (count($urls) !== 0) {
44
            $options = [
45 1
                CURLOPT_RETURNTRANSFER => true,
46
            ];
47
48
            // Add all curl handlers and remember them
49
            // Initiate the multi curl handler
50 1
            $mh = curl_multi_init();
51 1
            $chAll = [];
52 1
            foreach ($urls as $url) {
53 1
                $ch = curl_init("$url");
54 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 ignore-type  annotation

54
                curl_setopt_array(/** @scrutinizer ignore-type */ $ch, $options);
Loading history...
55 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 ignore-type  annotation

55
                curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
56 1
                $chAll[] = $ch;
57
            }
58
59
            // Execute all queries simultaneously,
60
            // and continue when all are complete
61 1
            $running = null;
62
            do {
63 1
                curl_multi_exec($mh, $running);
64 1
            } while ($running);
65
66
            // Close the handles
67 1
            foreach ($chAll as $ch) {
68 1
                curl_multi_remove_handle($mh, $ch);
69
            }
70 1
            curl_multi_close($mh);
71
72
            // All of our requests are done, we can now access the results
73 1
            foreach ($chAll as $ch) {
74 1
                $data = curl_multi_getcontent($ch);
75 1
                $response[] = json_decode($data, true);
76
            }
77
        }
78
79 1
        return $response;
80
    }
81
}
82