Skip to main content
Solved

Displaying klaviyo data


Forum|alt.badge.img+2
  • Problem Solver II
  • 20 replies

Hi

I am trying to get a profile based on an ID to display to the user. The error that I am getting is:

 

Curl(27) out of Memory

 

Here is the php (txt) file:

https://ecovib2d.com.au/workfromhome/EmailMarketing/Klaviyo_work/display_kalviyodata.txt

 

Thanks.

Best answer by Tudsy

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("&nbsp;",40)." Id ". str_repeat('&nbsp;', 40)."FirstName".str_repeat('&nbsp;',7)."LastName".str_repeat('&nbsp;',12)."Email Address";
    echo "<br>";
    echo str_repeat(".",115);
    echo "<br/>";
    
    /* Code to extract the data */
    
    $items = count($userDetails);
    
    
    
    
    if($userDetails){

 
 
 echo str_repeat("&nbsp;",15).$userDetails['data'][0]['id'].str_repeat("&nbsp;",5);
 echo str_repeat("&nbsp;",15).$userDetails['data'][0]['attributes']['first_name'].str_repeat("&nbsp;",10);
 echo str_repeat("&nbsp;",3).$userDetails['data'][0]['attributes'] ['last_name'].str_repeat("&nbsp;",5);
 echo str_repeat("&nbsp;",10).$userDetails['data'][0]['attributes']['email'];
 echo "<br>";
    
    
    
    
    }
    
    echo str_repeat(".",115);

    echo "</center>";
    

?>

View original
Did this topic or the replies in the thread help you find an answer to your question?

9 replies

chloe.strange
Community Manager
Forum|alt.badge.img+42
  • Community Manager
  • 430 replies
  • July 11, 2024

Hi @Tudsy

Can you provide a little more context about what steps you have already taken and what you are looking to accomplish?

~Chloe


Forum|alt.badge.img+31
  • Partner
  • 252 replies
  • July 12, 2024

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.


Forum|alt.badge.img+2
  • Author
  • Problem Solver II
  • 20 replies
  • July 12, 2024

Hi

 

Thanks for that.

I am trying to display a profiles info (Name, email address and id) based on  a email address.

Thanks.


Forum|alt.badge.img+2
  • Author
  • Problem Solver II
  • 20 replies
  • July 12, 2024

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>";
    
    

    
?>


 


Forum|alt.badge.img+2
  • Author
  • Problem Solver II
  • 20 replies
  • July 12, 2024

Hi

 

Just to add.

 

I am on a shared IP hosting environment.

 

Thanks.

 

Tudsy


Forum|alt.badge.img+2
  • Author
  • Problem Solver II
  • 20 replies
  • July 13, 2024

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.


Forum|alt.badge.img+31
  • Partner
  • 252 replies
  • July 13, 2024

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);
 


Forum|alt.badge.img+2
  • Author
  • Problem Solver II
  • 20 replies
  • July 14, 2024

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.


Forum|alt.badge.img+2
  • Author
  • Problem Solver II
  • 20 replies
  • Answer
  • July 15, 2024

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("&nbsp;",40)." Id ". str_repeat('&nbsp;', 40)."FirstName".str_repeat('&nbsp;',7)."LastName".str_repeat('&nbsp;',12)."Email Address";
    echo "<br>";
    echo str_repeat(".",115);
    echo "<br/>";
    
    /* Code to extract the data */
    
    $items = count($userDetails);
    
    
    
    
    if($userDetails){

 
 
 echo str_repeat("&nbsp;",15).$userDetails['data'][0]['id'].str_repeat("&nbsp;",5);
 echo str_repeat("&nbsp;",15).$userDetails['data'][0]['attributes']['first_name'].str_repeat("&nbsp;",10);
 echo str_repeat("&nbsp;",3).$userDetails['data'][0]['attributes'] ['last_name'].str_repeat("&nbsp;",5);
 echo str_repeat("&nbsp;",10).$userDetails['data'][0]['attributes']['email'];
 echo "<br>";
    
    
    
    
    }
    
    echo str_repeat(".",115);

    echo "</center>";
    

?>