Issues (365)

src/Library/libraries/sylouuu/Curl/Curl.php (4 issues)

1
<?php
2
3
namespace sylouuu\Curl;
4
5
    /**
6
     * Curl abstracted class
7
     *
8
     * @author sylouuu
9
     * @link https://github.com/sylouuu/php-curl
10
     * @version 0.8.1
11
     * @license MIT
12
     */
13
    abstract class Curl
14
    {
15
        // Curl
16
        protected $ch;
17
        protected $curl_options;
18
19
        // Input
20
        protected $options;
21
22
        // Output
23
        protected $status;
24
        protected $header;
25
        protected $response;
26
27
        /**
28
         * Constructor
29
         *
30
         * @param string $url
31
         * @param array $options
32
         */
33
        public function __construct($url, $options = null)
34
        {
35
            if (isset($url)) {
36
                $this->options = $options;
37
38
                $this->options['request_headers'] = [];
39
40
                // Init cURL
41
                $this->ch = curl_init($url);
42
            }
43
        }
44
45
        /**
46
         * Getter HTTP status code
47
         *
48
         * @return integer
49
         */
50
        public function getStatus()
51
        {
52
            return $this->status;
53
        }
54
55
        /**
56
         * Getter HTTP header
57
         *
58
         * @return string
59
         */
60
        public function getHeader()
61
        {
62
            return $this->header;
63
        }
64
65
        /**
66
         * Getter response
67
         *
68
         * @return mixed
69
         */
70
        public function getResponse()
71
        {
72
            return $this->response;
73
        }
74
75
        /**
76
         * Getter Curl options
77
         *
78
         * @return null|array
79
         */
80
        public function getCurlOptions()
81
        {
82
            return $this->curl_options;
83
        }
84
85
        /**
86
         * Setter Curl option
87
         *
88
         * See options list: http://php.net/manual/en/function.curl-setopt.php
89
         *
90
         * @param const $option
91
         * @param mixed $value
92
         * @return mixed
93
         */
94
        public function setCurlOption($option, $value)
95
        {
96
            curl_setopt($this->ch, $option, $value);
0 ignored issues
show
$option of type sylouuu\Curl\const is incompatible with the type integer expected by parameter $option of curl_setopt(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            curl_setopt($this->ch, /** @scrutinizer ignore-type */ $option, $value);
Loading history...
97
98
            $this->curl_options[$option] = $value;
99
100
            return $this;
101
        }
102
103
        /**
104
         * Getter Curl info
105
         *
106
         * See info list: http://php.net/manual/en/function.curl-getinfo.php
107
         *
108
         * @param const $info
109
         * @return mixed
110
         */
111
        public function getCurlInfo($info)
112
        {
113
            return curl_getinfo($this->ch, $info);
0 ignored issues
show
$info of type sylouuu\Curl\const is incompatible with the type integer expected by parameter $opt of curl_getinfo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
            return curl_getinfo($this->ch, /** @scrutinizer ignore-type */ $info);
Loading history...
114
        }
115
116
        /**
117
         * Sends the request
118
         *
119
         * @return $this
120
         */
121
        public function send()
122
        {
123
            // Default options
124
            $this->setCurlOption(CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
sylouuu\Curl\CURLOPT_RETURNTRANSFER of type integer is incompatible with the type sylouuu\Curl\const expected by parameter $option of sylouuu\Curl\Curl::setCurlOption(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
            $this->setCurlOption(/** @scrutinizer ignore-type */ CURLOPT_RETURNTRANSFER, true);
Loading history...
125
            $this->setCurlOption(CURLINFO_HEADER_OUT, true);
126
127
            // Additional headers
128
            if (isset($this->options['headers']) && count($this->options['headers']) > 0) {
129
                $this->options['request_headers'] = array_merge($this->options['request_headers'], $this->options['headers']);
130
            }
131
132
            // SSL
133
            if (isset($this->options['ssl'])) {
134
                $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, true);
135
                $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, 2);
136
                $this->setCurlOption(CURLOPT_CAINFO, getcwd() . $this->options['ssl']);
137
            }
138
139
            // Payload
140
            if (isset($this->options['is_payload']) && $this->options['is_payload'] === true) {
141
                // Appropriate headers for sending a JSON object
142
                $this->options['request_headers'] = array_merge($this->options['request_headers'], [
143
                    'Content-Type: application/json',
144
                    'Content-Length: ' . ((isset($this->options['data'])) ? strlen(json_encode($this->options['data'])) : 0)
145
                ]);
146
            }
147
148
            // Set headers
149
            if (count($this->options['request_headers']) > 0) {
150
                $this->setCurlOption(CURLOPT_HTTPHEADER, $this->options['request_headers']);
151
            }
152
153
            // Retrieving HTTP response body
154
            $this->response = curl_exec($this->ch);
155
156
            // Retrieving HTTP status code
157
            $this->status = $this->getCurlInfo(CURLINFO_HTTP_CODE);
0 ignored issues
show
sylouuu\Curl\CURLINFO_HTTP_CODE of type integer is incompatible with the type sylouuu\Curl\const expected by parameter $info of sylouuu\Curl\Curl::getCurlInfo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
            $this->status = $this->getCurlInfo(/** @scrutinizer ignore-type */ CURLINFO_HTTP_CODE);
Loading history...
158
159
            // Retrieving HTTP header
160
            $this->header = $this->getCurlInfo(CURLINFO_HEADER_OUT);
161
162
            // Autoclose handle
163
            if (!isset($this->options['autoclose']) || (isset($this->options['autoclose']) && $this->options['autoclose'] !== false)) {
164
                $this->close();
165
            }
166
167
            return $this;
168
        }
169
170
        /**
171
         * Closes the handle
172
         */
173
        public function close()
174
        {
175
            curl_close($this->ch);
176
        }
177
    }
178