40 private $handles = [];
91 $this->maxAsync = $async;
92 $this->maxLoad = $load;
93 $this->multiHandle = curl_multi_init();
94 $this->verifySSL = empty($GLOBALS[
'egotec_conf'][
'proxy'][
'insecure']);
101 curl_multi_close($this->multiHandle);
112 $this->progress = $callback;
123 $this->start = $callback;
134 $this->finish = $callback;
145 $this->load = $callback;
153 private function next() {
154 if ($handle = array_shift($this->queue)) {
155 $this->handles[] = $handle;
156 if (is_callable($function = $this->start)) {
157 $function($handle[
'meta']);
159 curl_multi_add_handle($this->multiHandle, $handle[
'resource']);
171 if (!empty($this->handles)) {
175 if ($this->maxLoad > 0) {
177 if (is_callable($function = $this->load)) {
181 if ($load >= $this->maxLoad) {
186 if (curl_multi_select($this->multiHandle) == -1) {
190 $status = curl_multi_exec($this->multiHandle, $running);
191 }
while ($status == CURLM_CALL_MULTI_PERFORM);
193 while (($info = curl_multi_info_read($this->multiHandle)) !==
false) {
194 foreach ($this->handles as $index => $handle) {
195 if ($handle[
'resource'] == $info[
'handle']) {
196 array_splice($this->handles, $index, 1);
197 curl_multi_remove_handle($this->multiHandle, $handle[
'resource']);
198 if (is_callable($function = $this->finish)) {
199 $function($handle[
'meta'],
sizeof($this->handles),
sizeof($this->queue));
207 if (($this->maxAsync == 0 || $running < $this->maxAsync) && $this->next()) {
210 }
while ($running > 0 && $status == CURLM_OK);
222 public function add($url, $meta = []) {
223 $handle = curl_init();
225 curl_setopt($handle, CURLOPT_URL, $url);
226 curl_setopt($handle, CURLOPT_HEADER, 0);
227 curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
228 curl_setopt($handle, CURLOPT_COOKIE, EGOTEC .
'=' . session_id());
231 if (!$this->verifySSL) {
232 curl_setopt($handle, CURLOPT_SSL_VERIFYPEER,
false);
233 curl_setopt($handle, CURLOPT_SSL_VERIFYHOST,
false);
237 curl_setopt($handle, CURLOPT_HTTPHEADER, [
242 curl_setopt($handle, CURLOPT_WRITEFUNCTION,
function($handle, $data) {
243 if (is_callable($function = $this->progress)) {
244 foreach ($this->handles as $handle) {
245 if ($handle == $handle[
'resource']) {
246 $function($data, $handle[
'meta']);
252 return strlen($data);
256 parse_str(parse_url($url, PHP_URL_QUERY), $params);
259 'resource' => $handle,
260 'meta' => array_merge($params, $meta)
264 if ($this->maxAsync == 0 ||
sizeof($this->handles) < $this->maxAsync) {
__construct($async=0, $load=0.0)