4 private $_requestHeader = [];
31 $this->verifySSL = empty($GLOBALS[
'egotec_conf'][
'proxy'][
'insecure']);
34 if ($GLOBALS[
'egotec_conf'][
'proxy'][
'proxy_host']) {
36 $GLOBALS[
'egotec_conf'][
'proxy'][
'proxy_host'],
37 $GLOBALS[
'egotec_conf'][
'proxy'][
'proxy_port'],
38 $GLOBALS[
'egotec_conf'][
'proxy'][
'proxy_ssl'],
39 $GLOBALS[
'egotec_conf'][
'proxy'][
'proxy_login'],
40 $GLOBALS[
'egotec_conf'][
'proxy'][
'proxy_password'],
41 (
bool) $GLOBALS[
'egotec_conf'][
'proxy'][
'insecure']
53 foreach ($headers as $key => $value) {
54 $this->_requestHeader[$key] = $value;
65 public function auth($username, $password =
'') {
67 'Authorization' =>
'Basic '.base64_encode(
"$username:$password")
82 public function proxy($host, $port, $ssl =
false, $username =
'', $password =
'', $insecure =
false) {
83 $this->_proxy[
'host'] = $host;
84 $this->_proxy[
'port'] = (int) $port;
85 $this->_proxy[
'ssl'] = (bool) $ssl;
86 $this->_proxy[
'username'] = $username;
87 $this->_proxy[
'password'] = $password;
88 $this->_proxy[
'insecure'] = $insecure;
98 public function get($url, $additional_header = array(),
int $timeout = 0) {
102 'header' => $additional_header
103 ], $timeout > 0 ? [
'timeout' => $timeout] : []));
115 public function post($url, $param = array(), $additional_header = array(), $callback =
null) {
120 'header' => $additional_header,
121 'callback' => $callback
132 public function delete($url, $additional_header = array()) {
135 'method' =>
'DELETE',
136 'header' => $additional_header
148 public function put($url, $param = array(), $additional_header = array()) {
153 'header' => $additional_header
166 public function download($url, $target, $param = array(), $additional_header = array()) {
170 'download' => $target,
172 'header' => $additional_header
185 public function upload($url, $file, $param = array(), $additional_header = array()) {
191 'header' => $additional_header
205 $multiCurl = curl_multi_init();
208 foreach ($requests as $request) {
209 $ch = $this->createCurlHandle($request);
210 curl_multi_add_handle($multiCurl, $ch);
212 $curlHandles[] = $ch;
217 curl_multi_exec($multiCurl, $running);
218 curl_multi_select($multiCurl);
219 }
while ($running > 0);
221 foreach ($curlHandles as $ch) {
222 $response = curl_multi_getcontent($ch);
225 $this->multiError[] = curl_error($ch);
227 $response_headers = [];
228 $response_cookies = [];
229 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
230 $content_orig = substr($response, $header_size);
231 $headers = substr($response, 0, $header_size);
234 foreach (explode(
"\n", $headers) as $value) {
235 $pos = strpos($value,
':');
236 $response_headers[substr($value, 0, $pos)] = trim(substr($value, $pos + 1));
239 $content = $response_headers[
'Content-Encoding'] ==
'gzip' ? zlib_decode($content_orig) : $content_orig;
242 if ($response_headers[
'Set-Cookie']) {
243 foreach (explode(
"\n", $response_headers[
'Set-Cookie']) as $line) {
244 $rows = explode(
';', $line);
245 $row0_keyval = explode(
'=', array_shift($rows));
246 $cookie = [
'value' => $row0_keyval[1]];
248 foreach ($rows as $s) {
249 $keyval = explode(
'=', $s);
250 $cookie[trim($keyval[0])] = $keyval[1];
253 $response_cookies[$row0_keyval[0]] =
$cookie;
257 $info = curl_getinfo($ch);
259 $this->multiResponseHeader[] = $response_headers;
260 $this->multiCookie[] = $response_cookies;
261 $this->multiInfo[] =
$info;
262 $this->multiResponseTime[] =
$info[
'total_time'];
263 $this->multiCode[] =
$info[
'http_code'];
266 if (strpos($response_headers[
'Content-Type'],
'application/json') !==
false) {
267 $this->multiJson[] = json_decode(
$content);
271 curl_multi_remove_handle($multiCurl, $ch);
274 curl_multi_close($multiCurl);
284 $ch = $this->createCurlHandle($request);
286 $response = curl_exec($ch);
288 $this->error = curl_error($ch);
293 if (!$request[
'download']) {
295 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
296 $headers = substr($response, 0, $header_size);
297 $content_orig = substr($response, $header_size);
298 $this->responseHeader = array();
299 foreach (explode(
"\n", $headers) as $value) {
300 $pos = strpos($value,
":");
301 $key = substr($value, 0, $pos);
302 $val = trim(substr($value, $pos + 1));
303 $this->responseHeader[$key] = $val;
307 $this->cookie = array();
308 if ($this->responseHeader[
'Set-Cookie']) {
309 $cookies = explode(
"\n", $this->responseHeader[
'Set-Cookie']);
310 foreach ($cookies as $line) {
311 $row = explode(
";", $line);
312 $row0 = array_shift($row);
313 $row0_keyval = explode(
"=", $row0);
314 $cookie = array(
'value' => $row0_keyval[1]);
315 foreach ($row as $s) {
316 $keyval = explode(
"=", $s);
317 $cookie[trim($keyval[0])] = $keyval[1];
319 $this->cookie[$row0_keyval[0]] = $cookie;
324 $this->info = curl_getinfo($ch);
325 $this->responseTime = $this->info[
'total_time'];
326 $this->content = $this->responseHeader[
'Content-Encoding']==
'gzip' ? zlib_decode($content_orig) : $content_orig;
330 strpos($this->responseHeader[
'Content-Type'],
"application/json") !==
false
331 || strpos($this->responseHeader[
'content-type'],
"application/json") !==
false
333 $this->json = json_decode($this->content);
338 $this->code = $this->info[
'http_code'];
349 private function createCurlHandle(array $request) {
351 foreach (array_merge($this->_requestHeader, $request[
'header'] ?? []) as $key => $value) {
352 $header[] = $key .
': ' . $value;
357 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
358 curl_setopt($ch, CURLOPT_URL, $request[
'url']);
359 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request[
'method']);
361 if ($request[
'timeout']) {
362 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $request[
'timeout']);
363 curl_setopt($ch, CURLOPT_TIMEOUT, $request[
'timeout']);
367 if ($request[
'cookie']) {
368 curl_setopt($ch, CURLOPT_COOKIE, $request[
'cookie']);
372 if ($request[
'download']) {
373 $fp = fopen($request[
'download'],
'w+');
374 curl_setopt($ch, CURLOPT_FILE, $fp);
375 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
true);
376 curl_setopt($ch, CURLOPT_TIMEOUT, 900);
378 curl_setopt($ch, CURLOPT_HEADER, $this->returnHeader);
379 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
383 if (!$this->verifySSL) {
384 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,
false);
385 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,
false);
389 if ($request[
'upload']) {
390 curl_setopt($ch, CURLOPT_INFILE, $request[
'upload']);
391 curl_setopt($ch, CURLOPT_INFILESIZE, filesize($request[
'upload']));
395 if (!empty($request[
'param'])) {
396 assert($request[
'method'] ===
'POST',
'Makes only sense with POST.');
397 if ($this->use_http_build_query) {
398 if ($this->flatten_array_params) {
402 foreach ($request[
'param'] as $key => $value) {
403 if (is_array($value) && count(array_filter(array_keys($value),
'is_string')) == 0) {
404 foreach ($value as $v) {
405 $flatten[] =
"$key=" . rawurlencode($v);
408 $params[$key] = $value;
412 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)
413 . (
sizeof($flatten) > 0 ?
'&' . implode(
'&', $flatten) :
''));
415 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request[
'param']));
418 curl_setopt($ch, CURLOPT_POSTFIELDS, $request[
'param']);
423 if (!empty($this->_proxy)) {
424 curl_setopt($ch, CURLOPT_PROXY, $this->_proxy[
'host']);
425 curl_setopt($ch, CURLOPT_PROXYPORT, $this->_proxy[
'port']);
426 if ($this->_proxy[
'ssl']) {
427 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL,
true);
429 if (!empty($this->_proxy[
'username'])) {
430 curl_setopt($ch, CURLOPT_PROXYUSERPWD, implode(
':', [
431 $this->_proxy[
'username'],
432 $this->_proxy[
'password']
436 if ($this->_proxy[
'insecure']) {
437 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,
false);
438 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,
false);
443 if (!empty($request[
'callback']) && is_callable($request[
'callback'])) {
444 curl_setopt($ch, CURLOPT_WRITEFUNCTION,
function($ch, $str) use ($request) {
445 $request[
'callback']($str);
multiRequest(array $requests)
auth($username, $password='')
proxy($host, $port, $ssl=false, $username='', $password='', $insecure=false)
post($url, $param=array(), $additional_header=array(), $callback=null)
download($url, $target, $param=array(), $additional_header=array())
upload($url, $file, $param=array(), $additional_header=array())
put($url, $param=array(), $additional_header=array())