-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurlBase.php
More file actions
327 lines (283 loc) · 8.08 KB
/
Copy pathCurlBase.php
File metadata and controls
327 lines (283 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* CurlBase
*
* This class handles groups of CurlRequest objects. You can execute them concurrently
* or not, add default cURL options, attach default event callbacks and more.
*
* @package CurlObjects
* @author Robert Diaes
*/
class CurlBase {
/**
* If you are having problems with curl_multi or you just don't need concurrent requests,
* change to false and it will work with non-multi curl. You will still be able to execute
* multiple requests (but they won't be executed concurrently).
*
* @var bool Toggles use of the curl_multi interface.
*/
public $multi = true;
/**
* @var int Maximum number of requests that will run concurrently.
*/
public $maxChunk = 25;
/**
* @var int Delay between requests in seconds, only works with $multi set to false.
*/
public $delaySingle = 0;
/**
* @var int Delay between chunks in seconds, only works with $multi set to true.
*/
public $delayChunks = 0;
/**
* @var int Function to be run between chunks, called with $this (CurlBase) as it's only argument.
*/
public $chunkCallback = null;
/**
* @var array The pool of CurlRequests.
*/
public $requests = array();
/**
* An associative array of currently active requests.
*
* @var array Request ID => cURL handle
*/
protected $active = array();
/**
* Default cURL Options
*
* These options will be added to each request object.
* Duplicate options will be overriden by values in CurlRequest::$options.
*
* @var array Format is CURL_CONSTANT => value
*/
public $defaultOptions = array(
//CURLOPT_RETURNTRANSFER => true
);
/**
* Default callbacks
*
* These callbacks will be attached to each request's corresponding event.
* Each sub-array will be given as arguments to the request's attach() method.
*
* @var array Format is (event_name, callable, position)
*/
public $defaultAttach = array(
//array($event, $callback, $position),
//array($event, $callback, $position)
);
/**
* @var array Stores errors regarding the cURL multi-handle.
*/
public $multiErrors = array();
/**
* @var resource The cURL multi-handle.
*/
protected $mh;
/**
* This is used for auto-generating request IDs. Will not re-use IDs if you remove requests.
*
* @var int Total number of requests that have been added to the pool.
*/
protected $rCount = 0;
/*
** Constructor
*/
public function __construct($requests=null,$options=null) {
if($requests)
$this->addArr($requests);
if($options)
$this->defaultOptions += $options;
}
/*
** Main methods
*/
// add a CurlRequest
public function add(&$request, $id=null) {
if(!isset($id)) { $id = $this->rCount; }
// Attach default callbacks
foreach($this->defaultAttach as $at) {
$request->attach($at[0], $at[1], $at[2]);
}
$this->requests[$id] = $request;
$this->requests[$id]->_id = $id;
$this->active($id, TRUE);
$this->rCount++;
return $id;
}
// add array of CurlRequests
public function addArray(&$requests) {
foreach($requests as $i => &$request) {
if(is_int($i)) { $i = $this->rCount; }
$ids[]=$i;
$this->add($request, $i);
}
return $ids;
}
protected function active($k, $active=null) {
if(is_null($active)) {
return array_key_exists($k, $this->active);
} else if($active === true) {
$this->active[$k] = $this->requests[$k]->_handle;
} else if($active === false) {
unset($this->active[$k]);
}
}
// execute added CurlRequests
public function perform() {
// check if there are active requests, must have been added after the last perform, other reqs in $this->requests won't count
if(($k = count($this->active)) > 0 ) {
//If there's only one request drop down to single mode
if($k == 1)
$this->multi = false;
// Initialise the multi-handle if we are in multi mode
if($this->multi) {
if(!($this->mh = curl_multi_init())) {
$this->multiErrors[] = "Failed to initialize multi handle";
return;
}
}
// Chunk and perform() once for each chunk, if we have more active requests than allowed to perform simultaneously
if($this->multi && $this->maxChunk < $k)
{
$chunks = array_chunk($this->active, $this->maxChunk, true);
foreach($chunks as $chunk) {
// assign current chunk as active
$this->active = $chunk;
// this will only loop when requests use $keep=true
do {
// perform requests in the pool
$this->performRequests();
} while(count($this->active)>0);
// Callback for cleanup between chunks
if($this->chunkCallback) {
// pass a reference to $this CurlBase as the only argument
call_user_func_array($this->chunkCallback, array($this));
}
// delay if needed
if($this->delayChunks > 0) {
sleep($this->delayChunks);
}
}
}
// If we only have one chunk
else {
// this will only loop when requests use $keep=true
do {
// perform requests in the active pool
$this->performRequests();
} while(count($this->active)>0);
}
// Close the multi handle
if($this->multi) {
curl_multi_close($this->mh);
}
}
}
/******
** Internal methods
******/
protected function addHandle($k) {
if(($n = curl_multi_add_handle($this->mh,$this->active[$k])) > 0) {
$this->active($k, FALSE);
$this->requests[$k]->event('curlerror', array(999, 'Failed to add to multihandle'));
}
}
protected function removeHandle($k) {
if(($n = curl_multi_remove_handle($this->mh,$this->active[$k])) === false) {
$this->active($k, FALSE);
$this->requests[$k]->event('curlerror', array(999, 'Failed to remove from multihandle'));
}
}
protected function setOptions($k) {
$req = $this->requests[$k];
if(curl_setopt_array($this->active[$k], $req->setOptions($this->defaultOptions)) === false) {
$req->event('curlerror', array(999, 'Failed to set options'));
$this->active($k, FALSE);
return false;
} else {
return true;
}
}
protected function prepareRequests() {
foreach(array_keys($this->active) as $k) {
$req = $this->requests[$k];
// Trigger before event
$req->event('before');
// If it's the first run, initialize the handle
if(!is_resource($req->_handle)) {
if(($handle = curl_init()) !== false) {
$req->_handle = $handle;
$this->active($k, TRUE);
if($this->setOptions($k)) {
$this->addHandle($k);
}
} else {
$this->active($k, FALSE);
}
} else {
if($this->setOptions($k)) {
$this->addHandle($k);
}
}
}
}
protected function performRequests() {
if($this->multi) {
$this->prepareRequests();
// Start performing the requests
$activeConnections = null;
do {
$mrc = curl_multi_exec($this->mh, $activeConnections);
} while($mrc == CURLM_CALL_MULTI_PERFORM);
while ($activeConnections > 0 && $mrc == CURLM_OK) {
// Wait for network
if (curl_multi_select($this->mh) != -1) {
// pull in any new data, or at least handle timeouts
do {
$mrc = curl_multi_exec($this->mh, $activeConnections);
} while( $mrc == CURLM_CALL_MULTI_PERFORM);
}
}
if ($mrc !== CURLM_OK) {
$this->multiErrors[] = "Curl multi read error: $mrc\n";
// return false;
}
}
foreach(array_keys($this->active) as $k) {
$req = $this->requests[$k];
if(!$this->multi) {
if($this->delaySingle > 0) {
sleep($this->delaySingle);
}
// Execute the request
$req->exec();
}
if($this->multi) {
if (($ern = curl_errno($this->active[$k])) === 0 ) {
$response = curl_multi_getcontent($this->active[$k]);
$info = curl_getinfo($this->active[$k]);
$req->execCount++;
$req->event('parse', array($response,$info));
$req->event('decide');
} else {
$msg = curl_error($this->active[$k]);
$req->event('curlerror', array($ern, $msg));
}
$this->removeHandle($k);
$req->event('after');
if($req->execCount > $req->maxExecCount) {
$req->event('maxexec', array($req->execCount));
}
}
if(!$req->keep) {
if(is_resource($this->active[$k])) {
curl_close($this->active[$k]);
}
$req->_handle = false;
$this->active($k, FALSE);
}
}
}
}
?>