Add wrapper around running curl (runCurl)

This commit is contained in:
Joshua Ramon Enslin 2020-08-05 16:36:02 +02:00 committed by Stefan Rohde-Enslin
parent 790e332502
commit c73d279ff0

View File

@ -115,4 +115,31 @@ class MD_STD {
}
/**
* Wrapper for curling contents from the web.
*
* @param string $url URL to query.
* @param integer $timeout Timeout in milliseconds.
*
* @return string
*/
public static function runCurl(string $url, int $timeout = 1200):string {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, $timeout); //timeout in seconds
curl_setopt($curl, CURLOPT_TIMEOUT_MS, $timeout); //timeout in seconds
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'md-bot/1.0');
$result = curl_exec($curl);
curl_close($curl);
if (is_bool($result)) return "";
return $result;
}
}