Add wrapper around running curl (runCurl)
This commit is contained in:
1 parent
790e332502
commit
c73d279ff0
1 file changed
+27
+27
@@ -115,4 +115,31 @@ public static function startsWithAny(string $haystack, array $needles):bool {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user