Curl::setOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php namespace CodeZero\Curl; 
2
3
class Curl
4
{
5
    /**
6
     * cURL Resource Handle
7
     *
8
     * @var resource
9
     */
10
    private $curl;
11
12
    /**
13
     * cURL Response
14
     *
15
     * @var bool|string
16
     */
17
    private $response;
18
19
    /**
20
     * Constructor
21
     *
22
     * @throws CurlException
23
     */
24
    public function __construct()
25
    {
26
        if ( ! extension_loaded('curl'))
27
        {
28
            throw new CurlException('The cURL extension is not installed');
29
        }
30
    }
31
32
    /**
33
     * Initialize a new cURL resource
34
     *
35
     * @return bool
36
     * @throws CurlException
37
     */
38
    public function initialize()
39
    {
40
        if ($this->isInitialized())
41
        {
42
            $this->close();
43
        }
44
45
        if ( ! ($this->curl = curl_init()))
46
        {
47
            throw new CurlException('Could not initialize a cURL resource');
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * Check if a cURL resource has been initialized
55
     *
56
     * @return bool
57
     */
58
    public function isInitialized()
59
    {
60
        return $this->curl != null;
61
    }
62
63
    /**
64
     * Set cURL option
65
     *
66
     * @param int $option
67
     * @param mixed $value
68
     *
69
     * @return bool
70
     * @throws CurlException
71
     */
72
    public function setOption($option, $value)
73
    {
74
        $this->autoInitialize();
75
76
        return curl_setopt($this->curl, $option, $value);
77
    }
78
79
    /**
80
     * Set cURL options
81
     *
82
     * @param array $options
83
     *
84
     * @return bool
85
     * @throws CurlException
86
     */
87
    public function setOptions(array $options)
88
    {
89
        $this->autoInitialize();
90
91
        return curl_setopt_array($this->curl, $options);
92
    }
93
94
    /**
95
     * Send the cURL request (will initialize if needed and set options if provided)
96
     *
97
     * !!! Options that have already been set are not automatically reset !!!
98
     *
99
     * @param array $options
100
     *
101
     * @return bool|string
102
     * @throws CurlException
103
     */
104
    public function sendRequest(array $options = [])
105
    {
106
        $this->autoInitialize();
107
108
        if ( ! empty($options))
109
        {
110
            if ( ! $this->setOptions($options))
111
            {
112
                return false;
113
            }
114
        }
115
116
        $this->response = curl_exec($this->curl);
117
118
        return $this->response;
119
    }
120
121
    /**
122
     * Get the response of the last cURL request
123
     *
124
     * @return bool|string
125
     */
126
    public function getResponse()
127
    {
128
        return $this->response;
129
    }
130
131
    /**
132
     * Get additional information about the last cURL request
133
     *
134
     * @param string $key
135
     *
136
     * @return string|array
137
     */
138
    public function getRequestInfo($key = null)
139
    {
140
        if ( ! $this->isInitialized())
141
        {
142
            return $key ? '' : [];
143
        }
144
145
        return $key ? curl_getinfo($this->curl, $key) : curl_getinfo($this->curl);
146
    }
147
148
    /**
149
     * Get the error from the last cURL request
150
     *
151
     * @return string
152
     */
153
    public function getError()
154
    {
155
        if ( ! function_exists('curl_strerror'))
156
        {
157
            return $this->getErrorDescription();
158
        }
159
160
        // PHP >= 5.5.0
161
        return curl_strerror($this->getErrorCode());
162
    }
163
164
    /**
165
     * Get the error code from the last cURL request
166
     *
167
     * @return int
168
     */
169
    public function getErrorCode()
170
    {
171
        return $this->curl ? curl_errno($this->curl) : 0;
172
    }
173
174
    /**
175
     * Get the error description from the last cURL request
176
     *
177
     * @return string
178
     */
179
    public function getErrorDescription()
180
    {
181
        return $this->curl ? curl_error($this->curl) : '';
182
    }
183
184
    /**
185
     * URL encodes the given string
186
     *
187
     * @param string $string
188
     *
189
     * @return string|bool
190
     * @throws CurlException
191
     */
192
    public function urlEncode($string)
193
    {
194
        return $this->parseUrl($string, false);
195
    }
196
197
    /**
198
     * Decodes the given URL encoded string
199
     *
200
     * @param string $string
201
     *
202
     * @return string|bool
203
     * @throws CurlException
204
     */
205
    public function urlDecode($string)
206
    {
207
        return $this->parseUrl($string, true);
208
    }
209
210
    /**
211
     * Reset all cURL options
212
     *
213
     * @return void
214
     * @throws CurlException
215
     */
216
    public function reset()
217
    {
218
        if ( ! $this->isInitialized() || ! function_exists('curl_reset'))
219
        {
220
            $this->initialize();
221
        }
222
        else
223
        {
224
            // PHP >= 5.5.0
225
            curl_reset($this->curl);
226
227
            $this->response = null;
228
        }
229
    }
230
231
    /**
232
     * Close the cURL resource
233
     *
234
     * @return void
235
     */
236
    public function close()
237
    {
238
        if ($this->isInitialized())
239
        {
240
            curl_close($this->curl);
241
242
            $this->curl = null;
243
            $this->response = null;
244
        }
245
    }
246
247
    /**
248
     * Get the cURL version
249
     *
250
     * @return string
251
     */
252
    public function getVersion()
253
    {
254
        return curl_version()['version'];
255
    }
256
257
    /**
258
     * Destructor
259
     */
260
    public function __destruct()
261
    {
262
        $this->close();
263
    }
264
265
    /**
266
     * Initialize cURL if it has not been initialized yet
267
     *
268
     * @throws CurlException
269
     */
270
    private function autoInitialize()
271
    {
272
        if ( ! $this->isInitialized())
273
        {
274
            $this->initialize();
275
        }
276
    }
277
278
    /**
279
     * Encode or decode a URL
280
     *
281
     * @param string $string
282
     * @param bool $decode
283
     *
284
     * @return bool|string
285
     */
286
    private function parseUrl($string, $decode)
287
    {
288
        $this->autoInitialize();
289
290
        $function = $decode ? 'curl_unescape' : 'curl_escape';
291
292
        if ( ! function_exists($function))
293
        {
294
            return $decode ? rawurldecode($string) : rawurlencode($string);
295
        }
296
297
        // PHP >= 5.5.0
298
        return call_user_func($function, $this->curl, $string);
299
    }
300
}
301