From c73d279ff02e7ac3ff7129d1fecabdcc8de22ec9 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Wed, 5 Aug 2020 16:36:02 +0200 Subject: [PATCH] Add wrapper around running curl (runCurl) --- MD_STD.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/MD_STD.php b/MD_STD.php index 2bb1f55..9fc625f 100644 --- a/MD_STD.php +++ b/MD_STD.php @@ -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; + + } + }