Last active
June 9, 2021 07:31
-
-
Save nikitos-one/384ee1e09cc3a926a7b5b1dc7892ec6e to your computer and use it in GitHub Desktop.
CURL PHP Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Request | |
{ | |
public static function query($requestMethod, $requestUrl, $requestData='', $requestHeader='') | |
{ | |
$curl=curl_init(); | |
$curl_param = [ | |
CURLOPT_SSL_VERIFYHOST => 0, | |
CURLOPT_SSL_VERIFYPEER => 0, | |
CURLOPT_AUTOREFERER => true, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_CUSTOMREQUEST => $requestMethod, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => '', | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_VERBOSE => true | |
]; | |
if($requestHeader) { | |
$curl_param += [ | |
CURLOPT_HTTPHEADER => $requestHeader | |
] ; | |
} | |
if (in_array($requestMethod, ['POST', 'PUT'])) { | |
$curl_param += [ | |
CURLOPT_URL => $requestUrl, | |
CURLOPT_POSTFIELDS => $requestData | |
]; | |
} | |
else { | |
$curl_param += [ | |
CURLOPT_URL => $requestUrl . '?' . http_build_query($requestData) | |
]; | |
} | |
curl_setopt_array($curl, $curl_param); | |
$result = curl_exec($curl); | |
if ($result === false) { | |
die('Curl failed: ' . curl_error($curl)); | |
} | |
curl_close($curl); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment