Skip to content

Instantly share code, notes, and snippets.

@cyberwombat
Created November 9, 2021 17:42
Show Gist options
  • Save cyberwombat/13efcab053ffd72de39f82832efd7728 to your computer and use it in GitHub Desktop.
Save cyberwombat/13efcab053ffd72de39f82832efd7728 to your computer and use it in GitHub Desktop.
<?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