EGOCMS  24.0
EGOTEC Content-Managament-System
Ego_HttpAsync.php
gehe zur Dokumentation dieser Datei
1 <?php
19  private $maxAsync;
20 
26  private $maxLoad;
27 
33  public $verifySSL = true;
34 
40  private $handles = [];
41 
47  private $queue = [];
48 
54  private $multiHandle;
55 
61  private $progress;
62 
68  private $start;
69 
75  private $finish;
76 
82  private $load;
83 
90  public function __construct($async = 0, $load = 0.0) {
91  $this->maxAsync = $async;
92  $this->maxLoad = $load;
93  $this->multiHandle = curl_multi_init();
94  $this->verifySSL = empty($GLOBALS['egotec_conf']['proxy']['insecure']);
95  }
96 
100  public function __destruct() {
101  curl_multi_close($this->multiHandle);
102  }
103 
111  public function setProgress($callback) {
112  $this->progress = $callback;
113  }
114 
122  public function setStart($callback) {
123  $this->start = $callback;
124  }
125 
133  public function setFinish($callback) {
134  $this->finish = $callback;
135  }
136 
144  public function setLoad($callback) {
145  $this->load = $callback;
146  }
147 
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']);
158  }
159  curl_multi_add_handle($this->multiHandle, $handle['resource']);
160  return true;
161  }
162  return false;
163  }
164 
170  public function run() {
171  if (!empty($this->handles)) {
172  $running = 0;
173 
174  do {
175  if ($this->maxLoad > 0) {
176  $load = Ego_System::getLoadAverage();
177  if (is_callable($function = $this->load)) {
178  $function($load);
179  }
180 
181  if ($load >= $this->maxLoad) {
182  sleep(5);
183  }
184  }
185 
186  if (curl_multi_select($this->multiHandle) == -1) {
187  usleep(100);
188  }
189  do {
190  $status = curl_multi_exec($this->multiHandle, $running);
191  } while ($status == CURLM_CALL_MULTI_PERFORM);
192 
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));
200  }
201  break;
202  }
203  }
204  }
205 
206  // Den nächsten wartenden Aufruf in die auszuführende Liste setzen
207  if (($this->maxAsync == 0 || $running < $this->maxAsync) && $this->next()) {
208  $running++;
209  }
210  } while ($running > 0 && $status == CURLM_OK);
211  }
212  }
213 
222  public function add($url, $meta = []) {
223  $handle = curl_init();
224 
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());
229 
230  // SSL Zertifikat nicht verifizieren
231  if (!$this->verifySSL) {
232  curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
233  curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
234  }
235 
236  // Der User-Agent kann auf "EGOCMS" abgefragt werden, um die Ausgabe zu manipulieren
237  curl_setopt($handle, CURLOPT_HTTPHEADER, [
238  'User-Agent: EGOCMS'
239  ]);
240 
241  // Callback Funktion ausführen
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']);
247  break;
248  }
249  }
250  }
251 
252  return strlen($data);
253  });
254 
255  // Alle URL Parameter werden als Meta-Daten weitergereicht
256  parse_str(parse_url($url, PHP_URL_QUERY), $params);
257 
258  $this->queue[] = [
259  'resource' => $handle,
260  'meta' => array_merge($params, $meta)
261  ];
262 
263  // Aufruf direkt in die auszuführende Liste setzen
264  if ($this->maxAsync == 0 || sizeof($this->handles) < $this->maxAsync) {
265  $this->next();
266  }
267  }
268 }
setLoad($callback)
setStart($callback)
setFinish($callback)
__construct($async=0, $load=0.0)
setProgress($callback)
add($url, $meta=[])
static getLoadAverage()