Created
November 9, 2021 17:42
-
-
Save cyberwombat/13efcab053ffd72de39f82832efd7728 to your computer and use it in GitHub Desktop.
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 WC_XMP_Request | |
{ | |
/* | |
* Perform CURL request | |
*/ | |
public static function request($url, $params = array(), $apiKey = null) | |
{ | |
$options = array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_SSL_VERIFYHOST => false, | |
CURLOPT_HEADER => false, | |
); | |
$headers = array( | |
"Accept: application/json", | |
"Authorization: Bearer " . $apiKey, | |
); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | |
if (sizeof($params)) { | |
$options[CURLOPT_POSTFIELDS] = http_build_query($params); | |
} | |
$ch = curl_init(); | |
curl_setopt_array($ch, $options); | |
$result = curl_exec($ch); | |
if (!curl_errno($ch)) { | |
$http_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); | |
curl_close($ch); | |
if ($http_code === 200) { | |
$data = json_decode($result, true); | |
if (json_last_error() != JSON_ERROR_NONE) { | |
echo 'INVALID JSON'; | |
} | |
if (@$data['Error']) { | |
echo 'ERROR ' .$data['Error']; | |
} | |
return $data; | |
} else { | |
die('ERROR! XMP returned a '.$http_code.' error code'); | |
} | |
} else { | |
echo 'ERROR'; | |
$error_msg = curl_error($ch); | |
echo $error_msg; | |
curl_close($ch); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment