Repository navigation
Expand file tree
/
Copy pathRollingCurlMini.php
More file actions
235 lines (213 loc) · 7.31 KB
/
Copy pathRollingCurlMini.php
File metadata and controls
235 lines (213 loc) · 7.31 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
<?php
/**
* multi-curl functions wrapper class
* a fork of Rolling Curl (http://code.google.com/p/rolling-curl/)
*/
class RollingCurlMini
{
/**
* max. number of simultaneous connections allowed
*/
protected $nThreads = 10;
/**
* shared cURL options
*/
protected $aOptions = array();
/**
* shared cURL request headers
*/
protected $aHeaders = array();
/**
* default callback (called as each request is completed)
*/
protected $fnCallback = 0;
/**
* timeout used for curl_multi_select function
*/
protected $nTimeout = 10;
/**
* the request queue
*/
protected $aRequests = array();
/**
* flag telling if the request queue execution has to be broken (aborted)
*/
protected $bBreak = false;
/**
* @param int $nThreads
*/
function __construct($nThreads = 0) {
$this->setThreads($nThreads);
}
/**
* @param int $nThreads
*/
function setThreads($nThreads) {
if ($nThreads > 0)
$this->nThreads = $nThreads;
}
/**
* @param array $aOptions
*/
function setOptions($aOptions) {
if (is_array($aOptions) && count($aOptions))
$this->aOptions = $aOptions;
}
/**
* @param array $aHeaders
*/
function setHeaders($aHeaders) {
if (is_array($aHeaders) && count($aHeaders))
$this->aHeaders = $aHeaders;
}
/**
* @param callback $fnCallback
*/
function setCallback($fnCallback) {
if (is_callable($fnCallback))
$this->fnCallback = $fnCallback;
}
/**
* @param int $nTimeout
*/
function setTimeout($nTimeout) {
if ($nTimeout > 0)
$this->nTimeout = $nTimeout;
}
/**
* Add a request to the request queue
* @param string $url - requested URL
* @param array|string|true $postdata - POST data
* @param callback $fnCallback - individual callback (called as a request is completed)
* @param mixed $userdata - user-defined data
* @param array $aOptions - individual cURL options
* @param array $aHeaders - individual cURL request headers
*/
function add($url, $postdata = 0, $fnCallback = 0, $userdata = 0,
$aOptions = 0, $aHeaders = 0) {
$this->aRequests[] = array(
$url, $postdata, $fnCallback? $fnCallback : $this->fnCallback, $userdata,
$aOptions, $aHeaders
);
}
/**
* @return int
*/
function count() {
return count($this->aRequests);
}
/**
* Reset the request queue
*/
function reset() {
$this->aRequests = array();
}
/**
* Execute the request queue
*/
function execute() {
$this->bBreak = false;
if (count($this->aRequests) < $this->nThreads)
$this->nThreads = count($this->aRequests);
// the request map that maps the request queue to request curl handles
$a_handles = $a_reqs_map = array();
$hcm = curl_multi_init();
// start processing the initial request queue
for ($i = 0; $i < $this->nThreads; $i++) {
$a_handles[$i] = $hc = curl_init();
curl_setopt_array($hc, $this->buildOptions($this->aRequests[$i]));
curl_multi_add_handle($hcm, $hc);
// add curl handle of a request to the request map
$key = (string) $hc;
$a_reqs_map[$key] = $i;
}
$n = $this->nThreads;
do {
while (($code = curl_multi_exec($hcm, $flag)) == CURLM_CALL_MULTI_PERFORM) ;
if ($code != CURLM_OK)
break;
// a request is just completed, find out which one
while ($a_done = curl_multi_info_read($hcm)) {
$hc = $a_done['handle'];
$output = curl_multi_getcontent($hc);
$a_info = curl_getinfo($hc);
if (!(curl_errno($hc) == 0 && intval($a_info['http_code']) == 200))
$output = 0;
$key = (string) $hc;
$i = $a_reqs_map[$key];
list($url, , $callback, $userdata, $a_opts, ) = $this->aRequests[$i];
if ($output &&
(isset($this->aOptions[CURLOPT_HEADER]) || isset($a_opts[CURLOPT_HEADER]))) {
$k = intval($a_info['header_size']);
$a_info['response_header'] = substr($output, 0, $k);
$output = substr($output, $k);
}
// remove curl handle of completed request from multi handle:
curl_multi_remove_handle($hcm, $hc);
$this->aRequests[$i] = 0;
if (isset($a_handles[$i]))
unset($a_handles[$i]);
// call the callback function and pass response info and user data to it
if (is_callable($callback)) {
call_user_func($callback, $output, $url, $a_info, $userdata);
}
if ($this->bBreak) {
foreach ($a_handles as $i => $hc)
curl_multi_remove_handle($hcm, $hc);
break;
}
// add/start a new request to the request queue
if ($n < count($this->aRequests) && isset($this->aRequests[$n])) {
curl_setopt_array($hc, $this->buildOptions($this->aRequests[$n]));
curl_multi_add_handle($hcm, $hc);
// add curl handle of a new request to the request map
$a_reqs_map[$key] = $n;
$a_handles[$n] = $hc;
$n++;
}
else {
unset($a_reqs_map[$key]);
}
}
// wait for activity on any connection
if ($flag && !$this->bBreak)
curl_multi_select($hcm, $this->nTimeout);
}
while (($flag || count($a_reqs_map)) && !$this->bBreak);
$this->reset();
curl_multi_close($hcm);
}
/**
* Ask to break execution of the request queue
*/
function requestBreak() {
$this->bBreak = true;
}
/**
* Build individual cURL options for a request
* @param array $aRequest - request data
* @return array
*/
protected function buildOptions($aRequest) {
list($url, $postdata, , , $a_opts, $a_hdrs) = $aRequest;
// merge shared and individual cURL options
$a_options = $a_opts? $a_opts + $this->aOptions : $this->aOptions;
// merge shared and individual request headers
$a_headers = $a_hdrs? $a_hdrs + $this->aHeaders : $this->aHeaders;
// set request URL
$a_options[CURLOPT_URL] = $url;
// set request headers
if ($a_headers)
$a_options[CURLOPT_HTTPHEADER] = $a_headers;
// set value of referer if it has not specified
if (!isset($a_options[CURLOPT_REFERER]) && !$a_options[CURLOPT_REFERER])
$a_options[CURLOPT_REFERER] = substr($url, 0, strrpos($url, '/') + 1);
// enable POST method and set POST parameters
if ($postdata) {
$a_options[CURLOPT_POST] = 1;
$a_options[CURLOPT_POSTFIELDS] = is_array($postdata)?
http_build_query($postdata) : $postdata;
}
return $a_options;
}
}