11<?php
22namespace PHPCurl \CurlWrapper ;
33
4- use InvalidArgumentException ;
5-
6- /**
7- * OOP wrapper for curl_* functions
8- *
9- * Functional and OOP style mapping:
10- *
11- * $h = curl_init($url); $curl = new Curl($url); //or $curl->init($url)
12- * curl_close($h); unset($curl);
13- * $e = curl_errno($h); $e = $curl->errno();
14- * $e = curl_error($h); $e = $curl->error();
15- * $i = curl_getinfo($h, $opt); $i = $curl->getInfo($opt);
16- * curl_setopt($h, $opt, $val); $curl->setOpt($opt, $val);
17- * curl_setopt_array($h, $array); $curl->setOptArray($array);
18- * curl_version($age) Curl::version($age);
19- * curl_strerror($errornum) Curl::strerror($errornum);
20- * $h2 = curl_copy_handle($h); $curl2 = clone($curl);
21- * $result = curl_exec($h); $result = $curl->exec();
22- * $res = curl_pause($h, $mask); $res = $curl->pause($mask);
23- * $res = curl_escape($h, $str); $res = $curl->escape($str);
24- * $res = curl_unescape($h, $str); $res = $curl->unescape($str);
25- *
26- * @copyright Alexey Karapetov
27- * @author Alexey Karapetov <karapetov@gmail.com>
28- * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
29- */
304class Curl
315{
326 /**
33- * curl handle
34- *
357 * @var resource
368 */
379 private $ handle ;
3810
3911 /**
40- * Ctor
41- *
4212 * @param string $url URL
4313 */
4414 public function __construct ($ url = null )
@@ -57,26 +27,6 @@ public function init($url = null)
5727 $ this ->handle = curl_init ($ url );
5828 }
5929
60- /**
61- * Get curl handle
62- *
63- * @return resource
64- */
65- public function getHandle ()
66- {
67- return $ this ->handle ;
68- }
69-
70- /**
71- * @see curl_close()
72- *
73- * @return void
74- */
75- public function __destruct ()
76- {
77- curl_close ($ this ->handle );
78- }
79-
8030 /**
8131 * @see curl_errno()
8232 *
@@ -100,29 +50,11 @@ public function error()
10050 /**
10151 * @see curl_exec()
10252 *
103- * @param int $attempts Connection attempts (default is 1)
104- * @param boolean $useException Throw \RuntimeException on failure
10553 * @return boolean|string
106- * @throws InvalidArgumentException if the number of attempts is invalid
107- * @throws CurlException if curl_exec() returned false
10854 */
109- public function exec ($ attempts = 1 , $ useException = false )
55+ public function exec ()
11056 {
111- $ attempts = (int ) $ attempts ;
112- if ($ attempts < 1 ) {
113- throw new InvalidArgumentException (sprintf ('Attempts count is not positive: %d ' , $ attempts ));
114- }
115- $ i = 0 ;
116- while ($ i ++ < $ attempts ) {
117- $ result = curl_exec ($ this ->handle );
118- if ($ result !== false ) {
119- break ;
120- }
121- }
122- if ($ useException && (false === $ result )) {
123- throw new CurlException (sprintf ('Error "%s" after %d attempt(s) ' , $ this ->error (), $ attempts ), $ this ->errno ());
124- }
125- return $ result ;
57+ return curl_exec ($ this ->handle );
12658 }
12759
12860 /**
@@ -168,7 +100,7 @@ public function setOptArray(array $options)
168100 * @param int $age
169101 * @return array
170102 */
171- static public function version ($ age = CURLVERSION_NOW )
103+ public function version ($ age = CURLVERSION_NOW )
172104 {
173105 return curl_version ($ age );
174106 }
@@ -177,24 +109,13 @@ static public function version($age = CURLVERSION_NOW)
177109 * @see curl_strerror()
178110 *
179111 * @param int $errornum
180- * @return array
112+ * @return string
181113 */
182- static public function strerror ($ errornum )
114+ public function strError ($ errornum )
183115 {
184116 return curl_strerror ($ errornum );
185117 }
186118
187- /**
188- * __clone
189- * Copies handle using curl_copy_handle()
190- *
191- * @return void
192- */
193- public function __clone ()
194- {
195- $ this ->handle = curl_copy_handle ($ this ->handle );
196- }
197-
198119 /**
199120 * @see curl_escape()
200121 *
@@ -236,4 +157,24 @@ public function pause($bitmask)
236157 {
237158 return curl_pause ($ this ->handle , $ bitmask );
238159 }
160+
161+ /**
162+ * Get curl handle
163+ *
164+ * @return resource
165+ */
166+ public function getHandle ()
167+ {
168+ return $ this ->handle ;
169+ }
170+
171+ public function __destruct ()
172+ {
173+ curl_close ($ this ->handle );
174+ }
175+
176+ public function __clone ()
177+ {
178+ $ this ->handle = curl_copy_handle ($ this ->handle );
179+ }
239180}
0 commit comments