public static function curlRequest($url, $params = [], $method = "GET", array $curlOptions = [])
{
$ch = curl_init();
$strParams = is_array($params) ? http_build_query($params) : $params;
// Method
switch (strtoupper($method)) {
default:
case "GET":
$url .= (strpos($url, "?") === FALSE ? "?" : "&") . $strParams;
break;
case "POST":
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
case "PUT":
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($ch, CURLOPT_POSTFIELDS, $strParams);
break;
}
// Secure Url
if (preg_match("!^https://!", $url)) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 1);
if (count($curlOptions)) {
curl_setopt_array($ch, $curlOptions);
}
$response = curl_exec($ch);
$headers = curl_getinfo($ch);
$data = ["response" => $response, "response_json" => json_decode($response, true), "headers" => $headers];
$error = curl_error($ch);
$errorNo = curl_errno($ch);
curl_close($ch);
if ($data["response"] === FALSE) {
throw new \Exception($error, $errorNo);
} else {
return $data;
}
}