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
| function getCurl(string $url, array $data = [], array $headers = [], bool $includeCookies = false): string { $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_AUTOREFERER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_TIMEOUT, 5000);
if ($includeCookies) { curl_setopt($curl, CURLOPT_HEADER, true); }
if (!empty($headers)) { curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); }
if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); }
$result = curl_exec($curl); if (curl_errno($curl)) { curl_close($curl); return ''; }
if ($includeCookies) { preg_match('/Set-Cookie:(.*);/iU', $result, $str); $result = $str[1] ?? ''; }
curl_close($curl); return $result; }
|