MastodonHelp/web/lib/gurl.php
2023-12-26 11:16:24 +01:00

32 lines
1.1 KiB
PHP

<?php
function gurl($url,$conntimeout,$timeout=45,$headers=[]) {
$curli=curl_init();
// i 2 qui sotto servono a far sì che ctrl+c sia intercettato a monte
/*curl_setopt($curli,CURLOPT_NOPROGRESS,false);
curl_setopt($curli,CURLOPT_PROGRESSFUNCTION,function($curli,10,10,10,10){});*/
curl_setopt($curli,CURLOPT_URL,$url);
curl_setopt($curli,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curli,CURLOPT_FAILONERROR,true);
curl_setopt($curli,CURLOPT_CONNECTTIMEOUT,$conntimeout);
curl_setopt($curli,CURLOPT_TIMEOUT,$timeout);
curl_setopt($curli,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curli,CURLOPT_HEADER,true);
curl_setopt($curli,CURLOPT_ENCODING,'');
$gheaders='';
$ecode=false;
$emsg=false;
$cont=curl_exec($curli);
if ($cont!==false) {
$gheaders_sz=curl_getinfo($curli,CURLINFO_HEADER_SIZE);
$gheaders=strtolower(substr($cont,0,$gheaders_sz));
$cont=substr($cont,$gheaders_sz);
} else {
$ecode=curl_errno($curli);
$emsg=$ecode.': '.trim(curl_error($curli));
}
curl_close($curli);
return(['cont'=>$cont,'headers'=>$gheaders,'ecode'=>$ecode,'emsg'=>$emsg]);
}
?>