i am trying to integrate my custom project with klaviyo api, i am using PHP and CURL for this matter, but i have run into the issue that i recieve the following from klaviyo api
array(1) { ["errors"]=> array(1) { [0]=> array(6) { ["id"]=> string(36) "bb41b9d1-8d18-485e-a8c4-704435f5e00e" ["status"]=> int(401) ["code"]=> string(17) "not_authenticated" ["title"]=> string(45) "Authentication credentials were not provided." ["detail"]=> string(68) "Missing or invalid authorization scheme. Please use Klaviyo-API-Key." ["source"]=> array(1) { ["pointer"]=> string(6) "/data/" } } } }
The error is easy to read, it ain't catching my API Key and can't authorize me...
My function that calls the API is the following, it seems like the CURL ain't sending the header correctly?
$system->Klaviyo($env['klaviyo_api'], 'GET', $env['klaviyo_url'] . 'profiles/?filter=equals(email,'.$orderData['recipient-email'].')', false));
public function Klaviyo($appId, $method, $url, $data){
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: ' . $appId,
'accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
'revision: 2023-12-15'
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
curl_close($curl);
if(!$result) {
$content = [];
$content['code'] = '000';
$content['status'] = 'error';
$content['message'] = 'Connection Failure';
$content['data'] = '';
} else {
$result = json_decode($result, true);
var_dump($result);
die();
if ( $result['status'] == 200 ) {
$content = [];
$content['code'] = '200';
$content['status'] = 'success';
$content['message'] = 'Connection Accepted';
$content['data'] = $result['data'];
} else {
$content = [];
$content['code'] = '404';
$content['status'] = 'error';
$content['message'] = 'Could not fetch data';
$content['data'] = '';
}
}
return json_encode($content);
}