| 1 |
<?php |
| 2 |
|
| 3 |
function acym_makeCurlCall(string $url, array $options = []): array |
| 4 |
{ |
| 5 |
$options['verifySsl'] = $options['verifySsl'] ?? true; |
| 6 |
|
| 7 |
$headers = []; |
| 8 |
if (!empty($options['headers']) && is_array($options['headers'])) { |
| 9 |
$headers = $options['headers']; |
| 10 |
} |
| 11 |
|
| 12 |
$data = []; |
| 13 |
if (!empty($options['data']) && is_array($options['data'])) { |
| 14 |
$data = $options['data']; |
| 15 |
} |
| 16 |
|
| 17 |
$allowedMethods = ['GET', 'POST']; |
| 18 |
$method = 'GET'; |
| 19 |
if (!empty($options['method']) && in_array($options['method'], $allowedMethods)) { |
| 20 |
$method = $options['method']; |
| 21 |
} |
| 22 |
|
| 23 |
$dataFormatted = ''; |
| 24 |
if (!empty($data)) { |
| 25 |
$isHeaderContentTypeJson = !empty($headers['Content-Type']) && $headers['Content-Type'] === 'application/json'; |
| 26 |
if ($method === 'POST' && $isHeaderContentTypeJson) { |
| 27 |
$dataFormatted = json_encode($data); |
| 28 |
} else { |
| 29 |
$dataFormatted = http_build_query($data); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
if ($method === 'GET' && !empty($dataFormatted)) { |
| 34 |
$url .= strpos($url, '?') === false ? '?'.$dataFormatted : '&'.$dataFormatted; |
| 35 |
} |
| 36 |
|
| 37 |
$ch = curl_init(); |
| 38 |
|
| 39 |
curl_setopt($ch, CURLOPT_URL, $url); |
| 40 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 41 |
if ($method === 'POST') { |
| 42 |
curl_setopt($ch, CURLOPT_POST, true); |
| 43 |
if (!empty($data)) { |
| 44 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataFormatted); |
| 45 |
} |
| 46 |
} |
| 47 |
if (!$options['verifySsl']) { |
| 48 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
| 49 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
| 50 |
} |
| 51 |
|
| 52 |
if (!empty($headers)) { |
| 53 |
$headersFormatted = array_map( |
| 54 |
function ($key, $value) { |
| 55 |
return $key.': '.$value; |
| 56 |
}, |
| 57 |
array_keys($headers), |
| 58 |
$headers |
| 59 |
); |
| 60 |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headersFormatted); |
| 61 |
} |
| 62 |
|
| 63 |
if (!empty($options['dns']) && is_string($options['dns'])) { |
| 64 |
curl_setopt($ch, CURLOPT_DNS_SERVERS, $options['dns']); |
| 65 |
} |
| 66 |
|
| 67 |
if (!empty($options['proxy']) && is_array($options['proxy'])) { |
| 68 |
curl_setopt($ch, CURLOPT_PROXY, $options['proxy']['host']); |
| 69 |
if (!empty($options['proxy']['auth'])) { |
| 70 |
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $options['proxy']['auth']); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$result = curl_exec($ch); |
| 75 |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 76 |
|
| 77 |
if (curl_errno($ch)) { |
| 78 |
$error = curl_error($ch); |
| 79 |
|
| 80 |
curl_close($ch); |
| 81 |
|
| 82 |
return ['error' => $error, 'status_code' => $httpCode]; |
| 83 |
} |
| 84 |
|
| 85 |
curl_close($ch); |
| 86 |
|
| 87 |
$result = json_decode($result, true); |
| 88 |
$result['status_code'] = $httpCode; |
| 89 |
|
| 90 |
return $result; |
| 91 |
} |
| 92 |
|
| 93 |
function acym_asyncCurlCall($urls) |
| 94 |
{ |
| 95 |
if (!function_exists('curl_multi_exec')) return; |
| 96 |
|
| 97 |
if (!is_array($urls)) $urls = [$urls]; |
| 98 |
|
| 99 |
try { |
| 100 |
$mh = curl_multi_init(); |
| 101 |
|
| 102 |
$handles = []; |
| 103 |
foreach ($urls as $url) { |
| 104 |
$ch = curl_init(); |
| 105 |
curl_setopt($ch, CURLOPT_URL, $url); |
| 106 |
curl_setopt($ch, CURLOPT_HEADER, 0); |
| 107 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 108 |
curl_multi_add_handle($mh, $ch); |
| 109 |
$handles[] = $ch; |
| 110 |
} |
| 111 |
|
| 112 |
$running = null; |
| 113 |
$time = 1; |
| 114 |
do { |
| 115 |
curl_multi_exec($mh, $running); |
| 116 |
usleep(100); |
| 117 |
if ($time > 50000) { |
| 118 |
break; |
| 119 |
} |
| 120 |
$time++; |
| 121 |
} while ($running); |
| 122 |
|
| 123 |
foreach ($handles as $handle) { |
| 124 |
curl_multi_remove_handle($mh, $handle); |
| 125 |
} |
| 126 |
curl_multi_close($mh); |
| 127 |
} catch (Exception $exception) { |
| 128 |
$config = acym_config(); |
| 129 |
$reportPath = $config->get('cron_savepath'); |
| 130 |
if (!empty($reportPath)) { |
| 131 |
$reportPath = str_replace(['{year}', '{month}'], [date('Y'), date('m')], $reportPath); |
| 132 |
$reportPath = acym_cleanPath(ACYM_ROOT.trim(html_entity_decode($reportPath))); |
| 133 |
acym_createDir(dirname($reportPath), true, true); |
| 134 |
|
| 135 |
$lr = "\r\n"; |
| 136 |
file_put_contents( |
| 137 |
$reportPath, |
| 138 |
$lr.$lr.'******************** '.acym_getDate( |
| 139 |
time() |
| 140 |
).' ********************'.$lr.'An error occurred while launching the multiple cron system, please make sure the PHP function "curl_multi_exec" is activated on your server: '.$exception->getMessage( |
| 141 |
), |
| 142 |
FILE_APPEND |
| 143 |
); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|