Hi @Tudsy,
Can you provide a little more context about what steps you have already taken and what you are looking to accomplish?
~Chloe
Hello @Tudsy You have exposed your Private Key on the URL. My suggestion would be to delete the URL as soon as possible and share the code directly in thread.
Hi
Thanks for that.
I am trying to display a profiles info (Name, email address and id) based on a email address.
Thanks.
Here is the code”
<?php
/* Trying to display a profiles data based on an email */
$apiKey = 'pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$email ='ecovib2d@live.com';
$data = array (
'api_key'=> $apiKey,
'email' => $email,
);
/* Get a profile id based on an email */
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://a.klaviyo.com/api/v2/people/search?email=".$email."&api_key=".$apiKey,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
"accept: application/json",
),
));
$response = curl_exec($curl);
if($errno = curl_errno($curl)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
echo " Go back to my website - ";
echo "<a href='https://ecovib2d.com.au/index.php' title='my website' alt='Error Message' >https://ecovib2d.com.au/index.php</a>";
}
curl_close($curl);
/* Get a profile based on an id in all lists - using the Get Profile API */
$apiKey = 'pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$datai = array (
'type' => 'profile',
'api_key' => $apiKey,
'id' => $response,
);
$curli = curl_init();
curl_setopt_array($curli,array(
CURLOPT_URL => "https://a.klaviyo.com/api/profiles/".$response."/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode($datai),
CURLOPT_HTTPHEADER => array(
" Authorization:".$apiKey,
" accept: application/json" ,
"revision: 2024-06-15",
),
));
$result = curl_exec($curli);
curl_close($curli);
if($errno = curl_errno($curli)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
echo " Go back to my website - ";
echo "<a href='https://ecovib2d.com.au/index.php' title='my website' alt='Error Message' >https://ecovib2d.com.au/index.php</a>";
}
echo "<br/>";
/* Extract and Print data */
$first_name = 'Adrian';
/* Display the results to the user */
echo "<center>";
echo $first_name." s data on Klaviyo ( Email Marketing Platform - www.klaviyo.com )";
echo "<br/>";
echo "...................................................................";
echo "<br/>";
echo " Id Firstname LastName Email Address ";
echo "...................................................................";
/* More work needs to be done here */
echo "<br/>";
echo "........................................................................................";
echo "</center>";
?>
Hi
Just to add.
I am on a shared IP hosting environment.
Thanks.
Tudsy
Hi
I am now updating to the latest api endpoints:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://a.klaviyo.com/api/profiles?filter=equals(email%2C%urlencode($email))&api_key=".$apiKey,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
"Authorization: pk_xxxxxxxxxxxxxxxxx",
"accept: application/json",
),
));
$id = curl_exec($curl);
The error now is:
cURL error (3): URL using bad/illegal format or missing URL
I don’t know what is wrong with this url?
Thanks.
Hello @Tudsy Try this code
$email = urlencode($email);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://a.klaviyo.com/api/profiles?filter=equals(email," . $email . ")",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $apiKey",
"accept: application/json",
),
));
$response = curl_exec($curl);
if ($response === false) {
$error = curl_error($curl);
curl_close($curl);
die('Curl error: ' . $error);
}
curl_close($curl);
$userDetails = json_decode($response, true);
print_r($userDetails);
Hi
I was wondering how to format the input ($data) for the api Add Profile to list?
I want to write the first name, last name and email address.
Thanks.
Hi
With the help of MaxBuzz, I have written a script (curl) that will display the id, first name, last name and the email address of a profile.
Here is the code:
<?php
$userDetails = array();
/* $email = urlencode($email); */
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://a.klaviyo.com/api/profiles?filter=equals(email,%22$email%22)",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
'Authorization: Klaviyo-API-Key pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'accept: application/json',
'revision: 2024-06-15',
),
));
$response = curl_exec($curl);
if ($response === false) {
$error = curl_error($curl);
curl_close($curl);
die('Curl error: ' . $error);
}
curl_close($curl);
$userDetails = json_decode($response, true);
/* Display the results to the user */
echo "<center>";
echo "<br>";
echo $first_name."s data on Klaviyo ( Email Marketing Platform - www.klaviyo.com )";
echo "<br/>";
echo "<br/>";
echo str_repeat(" ",40)." Id ". str_repeat(' ', 40)."FirstName".str_repeat(' ',7)."LastName".str_repeat(' ',12)."Email Address";
echo "<br>";
echo str_repeat(".",115);
echo "<br/>";
/* Code to extract the data */
$items = count($userDetails);
if($userDetails){
echo str_repeat(" ",15).$userDetails<'data'] 0]'id'].str_repeat(" ",5);
echo str_repeat(" ",15).$userDetailsa'data'];0]5'attributes']o'first_name'].str_repeat(" ",10);
echo str_repeat(" ",3).$userDetails&'data']p0],'attributes'] h'last_name'].str_repeat(" ",5);
echo str_repeat(" ",10).$userDetails"'data']s0]"'attributes']c'email'];
echo "<br>";
}
echo str_repeat(".",115);
echo "</center>";
?>