1 <?php
2
3 /**
4 * Object-oriented cURL wrapper
5 *
6 * @author Art <a.molcanovas@gmail.com>
7 */
8 class cURL {
9
10 /**
11 * The cURL resource
12 *
13 * @var resource
14 */
15 protected $ch;
16
17 /**
18 * Result of exec()
19 *
20 * @var mixed
21 * @see self::exec()
22 */
23 protected $exec;
24
25 /**
26 * Error number of exec()
27 *
28 * @var int
29 * @see self::exec()
30 */
31 protected $errno;
32
33 /**
34 * Error message of exec()
35 *
36 * @var string
37 * @see self::exec()
38 */
39 protected $error;
40
41 /**
42 * Whether the connection is open
43 *
44 * @var boolean
45 */
46 protected $is_open;
47
48 /**
49 * Instantiates the library
50 *
51 * @author Art <a.molcanovas@gmail.com>
52 *
53 * @param string $url Optionally, the URL for curl_init()
54 */
55 function __construct($url = null) {
56 if(!function_exists('curl_init')) {
57 trigger_error('cURL not available', E_USER_ERROR);
58 } else {
59 $this->ch = curl_init();
60 $this->is_open = true;
61 curl_setopt_array($this->ch,
62 [
63 CURLOPT_RETURNTRANSFER => true,
64 CURLOPT_SSL_VERIFYPEER => false,
65 CURLOPT_FOLLOWLOCATION => true,
66 CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36'
67 ]);
68
69 if($url) {
70 $this->setURL($url);
71 }
72 }
73 }
74
75 /**
76 * Sets the connection URL
77 *
78 * @author Art <a.molcanovas@gmail.com>
79 *
80 * @param string $url The URL
81 *
82 * @return cURL
83 */
84 function setURL($url) {
85 curl_setopt($this->ch, CURLOPT_URL, $url);
86
87 return $this;
88 }
89
90 /**
91 * A static wrapper function for __construct()
92 *
93 * @author Art <a.molcanovas@gmail.com>
94 *
95 * @param string $url Optionally, the URL for curl_init()
96 *
97 * @return cURL
98 */
99 static function init($url = null) {
100 return new cURL($url);
101 }
102
103 /**
104 * Sets CURLOPT_NOPROGRESS to FALSE and supplies the progress function
105 *
106 * @author Art <a.molcanovas@gmail.com>
107 *
108 * @param callable $callable The function
109 *
110 * @return bool
111 */
112 function setProgressFunction($callable) {
113 if(!is_callable($callable)) {
114 return false;
115 } else {
116 curl_setopt_array($this->ch,
117 [
118 CURLOPT_NOPROGRESS => false,
119 CURLOPT_PROGRESSFUNCTION => $callable
120 ]);
121
122 return true;
123 }
124 }
125
126 /**
127 * Set whether cURL should time out
128 *
129 * @author Art <a.molcanovas@gmail.com>
130 *
131 * @param boolean $enabled The switch
132 *
133 * @return cURL
134 */
135 function notimeout($enabled = true) {
136 $a = $enabled ? [
137 CURLOPT_CONNECTTIMEOUT => 86400,
138 CURLOPT_TIMEOUT => 86400
139 ] : [
140 CURLOPT_CONNECTTIMEOUT => 1,
141 CURLOPT_TIMEOUT => 1
142 ];
143
144 return $this->setopt_array($a);
145 }
146
147 /**
148 * Sets an array of options
149 *
150 * @author Art <a.molcanovas@gmail.com>
151 *
152 * @param array $a An array specifying which options to set and their values. The keys should be valid
153 * curl_setopt() constants or their integer equivalents.
154 *
155 * @return cURL
156 * @link http://php.net/manual/en/function.curl-setopt-array.php
157 */
158 function setopt_array(array $a) {
159 curl_setopt_array($this->ch, $a);
160
161 return $this;
162 }
163
164 /**
165 * Toggles lax SSL verification mode which doesn't check certificates
166 *
167 * @author Art <a.molcanovas@gmail.com>
168 *
169 * @param boolean $enabled Whether the mode is enabled or disabled
170 *
171 * @return cURL
172 */
173 function laxSSLMode($enabled = true) {
174 curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, !$enabled);
175
176 return $this;
177 }
178
179 /**
180 * Returns a string representation of the object data
181 *
182 * @author Art <a.molcanovas@gmail.com>
183 * @return string
184 */
185 function __toString() {
186 ob_start();
187 var_dump($this);
188
189 return ob_get_clean();
190 }
191
192 /**
193 * URL encodes the given string
194 *
195 * @author Art <a.molcanovas@gmail.com>
196 *
197 * @param string $str The string
198 *
199 * @return string The escaped string
200 * @link http://php.net/manual/en/function.curl-escape.php
201 */
202 function escape($str) {
203 return curl_escape($this->ch, $str);
204 }
205
206 /**
207 * Pause and unpause a connection
208 *
209 * @author Art <a.molcanovas@gmail.com>
210 *
211 * @param int $bitmask One of CURLPAUSE_* constants.
212 *
213 * @return int An error code (CURLE_OK for no error).
214 * @link http://php.net/manual/en/function.curl-pause.php
215 */
216 function pause($bitmask) {
217 return curl_pause($this->ch, $bitmask);
218 }
219
220 /**
221 * Gets cURL version information
222 *
223 * @author Art <a.molcanovas@gmail.com>
224 *
225 * @param int $age
226 *
227 * @return array
228 * @link http://php.net/manual/en/function.curl-version.php
229 */
230 function version($age = CURLVERSION_NOW) {
231 return curl_version($age);
232 }
233
234 /**
235 * Checks whether the last transfer was successful
236 *
237 * @author Art <a.molcanovas@gmail.com>
238 * @return boolean|int If successful - true, if not & cURL error code exists
239 * - cURL error code, false otherwise
240 */
241 function wasSuccessful() {
242 if($this->errno !== CURLE_OK) {
243 return 'cURL error #' . $this->errno;
244 } else {
245 $code = $this->getinfo(CURLINFO_HTTP_CODE);
246 if(!in_array(substr('' . $this->getinfo(CURLINFO_HTTP_CODE), 0, 1), [1, 2, 3])) {
247 return 'HTML response ' . $code;
248 } else {
249 return true;
250 }
251 }
252 }
253
254 /**
255 * Get information regarding a specific transfer
256 *
257 * @author Art <a.molcanovas@gmail.com>
258 *
259 * @param int $opt One of the cURL constants
260 *
261 * @return mixed
262 * @link http://php.net/manual/en/function.curl-getinfo.php
263 */
264 function getinfo($opt = 0) {
265 return curl_getinfo($this->ch, $opt);
266 }
267
268 /**
269 * Reset all options of a libcurl session handle
270 *
271 * @author Art <a.molcanovas@gmail.com>
272 * @return cURL
273 * @link http://php.net/manual/en/function.curl-reset.php
274 */
275 function reset() {
276 curl_reset($this->ch);
277 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
278
279 return $this;
280 }
281
282 /**
283 * Decodes the given URL encoded string
284 *
285 * @author Art <a.molcanovas@gmail.com>
286 *
287 * @param string $str
288 *
289 * @return string The decoded string
290 * @link http://php.net/manual/en/function.curl-unescape.php
291 */
292 function unescape($str) {
293 return curl_unescape($this->ch, $str);
294 }
295
296 /**
297 * Auto-cleanup
298 *
299 * @author Art <a.molcanovas@gmail.com>
300 */
301 function __destruct() {
302 $this->close();
303 }
304
305 /**
306 * Closes a cURL connection
307 *
308 * @author Art <a.molcanovas@gmail.com>
309 * @return cURL
310 * @link http://php.net/manual/en/function.curl-close.php
311 */
312 function close() {
313 if($this->is_open) {
314 @curl_close($this->ch);
315 $this->is_open = false;
316 }
317
318 return $this;
319 }
320
321 /**
322 * Gets the results of a cURL exec. If $url is set, will exec on that URL
323 *
324 * @author Art <a.molcanovas@gmail.com>
325 *
326 * @param string $url Optional URL override
327 *
328 * @return mixed The results of exec()
329 */
330 function get($url = null) {
331 if($url !== null) {
332 $this->setURL($url)->exec();
333 }
334
335 return $this->exec;
336 }
337
338 /**
339 * Executes the cURL connection parameters
340 *
341 * @author Art <a.molcanovas@gmail.com>
342 * @return cURL
343 * @link http://php.net/manual/en/function.curl-exec.php
344 */
345 function exec() {
346 $this->exec = curl_exec($this->ch);
347 $this->errno = curl_errno($this->ch);
348 $this->error = curl_error($this->ch);
349
350 return $this;
351 }
352
353 /**
354 * Returns the error message of the last exec()
355 *
356 * @author Art <a.molcanovas@gmail.com>
357 * @return string
358 * @see self::exec()
359 * @link http://php.net/manual/en/function.curl-error.php
360 */
361 function error() {
362 return $this->error;
363 }
364
365 /**
366 * Returns the error number of the last exec() or 0 if no error occurred
367 *
368 * @author Art <a.molcanovas@gmail.com>
369 * @return int
370 * @see self::exec()
371 * @link http://php.net/manual/en/function.curl-errno.php
372 */
373 function errno() {
374 return $this->errno;
375 }
376
377 /**
378 * Wrapper for setopt()
379 *
380 * @author Art <a.molcanovas@gmail.com>
381 *
382 * @param string $name Param name
383 * @param mixed $value Param value
384 */
385 function __set($name, $value) {
386 $this->setopt($name, $value);
387 }
388
389 /**
390 * Sets an option
391 *
392 * @author Art <a.molcanovas@gmail.com>
393 *
394 * @param int $name The option - see cURL constants
395 * @param mixed $value The option value
396 *
397 * @return cURL
398 * @link http://php.net/manual/en/function.curl-setopt.php
399 */
400 function setopt($name, $value) {
401 if($name === CURLOPT_POSTFIELDS) {
402 curl_setopt($this->ch, CURLOPT_POST, true);
403 }
404
405 curl_setopt($this->ch, $name, $value);
406
407 return $this;
408 }
409
410 }