1
1
<?php
2
2
namespace PHPCurl \CurlWrapper ;
3
3
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
- */
30
4
class Curl
31
5
{
32
6
/**
33
- * curl handle
34
- *
35
7
* @var resource
36
8
*/
37
9
private $ handle ;
38
10
39
11
/**
40
- * Ctor
41
- *
42
12
* @param string $url URL
43
13
*/
44
14
public function __construct ($ url = null )
@@ -57,26 +27,6 @@ public function init($url = null)
57
27
$ this ->handle = curl_init ($ url );
58
28
}
59
29
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
-
80
30
/**
81
31
* @see curl_errno()
82
32
*
@@ -100,29 +50,11 @@ public function error()
100
50
/**
101
51
* @see curl_exec()
102
52
*
103
- * @param int $attempts Connection attempts (default is 1)
104
- * @param boolean $useException Throw \RuntimeException on failure
105
53
* @return boolean|string
106
- * @throws InvalidArgumentException if the number of attempts is invalid
107
- * @throws CurlException if curl_exec() returned false
108
54
*/
109
- public function exec ($ attempts = 1 , $ useException = false )
55
+ public function exec ()
110
56
{
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 );
126
58
}
127
59
128
60
/**
@@ -168,7 +100,7 @@ public function setOptArray(array $options)
168
100
* @param int $age
169
101
* @return array
170
102
*/
171
- static public function version ($ age = CURLVERSION_NOW )
103
+ public function version ($ age = CURLVERSION_NOW )
172
104
{
173
105
return curl_version ($ age );
174
106
}
@@ -177,24 +109,13 @@ static public function version($age = CURLVERSION_NOW)
177
109
* @see curl_strerror()
178
110
*
179
111
* @param int $errornum
180
- * @return array
112
+ * @return string
181
113
*/
182
- static public function strerror ($ errornum )
114
+ public function strError ($ errornum )
183
115
{
184
116
return curl_strerror ($ errornum );
185
117
}
186
118
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
-
198
119
/**
199
120
* @see curl_escape()
200
121
*
@@ -236,4 +157,24 @@ public function pause($bitmask)
236
157
{
237
158
return curl_pause ($ this ->handle , $ bitmask );
238
159
}
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
+ }
239
180
}
0 commit comments