Skip to main content
Solved

PHP SDK: getProfiles by email AND return lists?


Forum|alt.badge.img+3
  • Problem Solver I
  • 8 replies

HI, I’m using PHP SDK 8.0.

I need to check if a profile exists for a specific email address and also find out which lists the profile is subscribed too. I’m trying to do it in a single API call if possible.

Below is my current PHP that returns the Klaviyo profile for an email address if it exists,.
It also indicates if the profile is subscribed to email marketing or not, but not which list the profile is subscribed to.

How can I find out which lists the profile is subscribed to? Can it be done using GetProfiles?

Many thanks
Stef
 


/**
 * GET KLAVIYO PROFILE BY EMAIL ADDRESS
 *
 * @param string $email The email address to search for
 * @param ProfilesApi $klaviyo The Klaviyo API instance
 * @return array|null The profile data if found, or null if not found
 */
function get_profiles_by_email($email, $klaviyo) {

    $logger = wc_get_logger();
    $context = ['source' => 'klaviyo_api'];

    $additional_fields_profile = 'subscriptions';
    $fields_profile = null;
    $filter ='equals(email,"'. $email .'")';
    $page_cursor = null;
    $page_size = 1;
    $sort = null;

    try {
        $response = $klaviyo->Profiles->getProfiles(
            $additional_fields_profile, 
            $fields_profile, 
            $filter, 
            $page_cursor, 
            $page_size, 
            $sort        
        );

        if (!empty($response['data'])) {
            // Profile found
            $profile = $response['data'][0];
            $logger->info("Profile found for email: $email", $context);
            return $profile;
        } else {
            // Profile not found
            $logger->info("Profile not found for email: $email", $context);
            return null;
        }
    } catch (Exception $e) {
        // Handle exceptions
        $logger->error('Klaviyo API "rjg_get_profiles_by_email" error: ' . $e->getMessage(), $context);
        return null;
    }
}

 

Best answer by Kim Strauch

This is possible in two API calls. 

  1. Filter profiles by email address, as you’re doing already. 
  2. Once you have a profile ID, you can use getProfileLists (https://github.com/klaviyo/klaviyo-api-php?tab=readme-ov-file#get-profile-lists) to get the lists for that profile 
View original
Did this topic or the replies in the thread help you find an answer to your question?

2 replies

Kim Strauch
Klaviyo Employee
Forum|alt.badge.img+9
  • Klaviyo Employee
  • 91 replies
  • Answer
  • June 3, 2024

This is possible in two API calls. 

  1. Filter profiles by email address, as you’re doing already. 
  2. Once you have a profile ID, you can use getProfileLists (https://github.com/klaviyo/klaviyo-api-php?tab=readme-ov-file#get-profile-lists) to get the lists for that profile 

Forum|alt.badge.img+3
  • Author
  • Problem Solver I
  • 8 replies
  • June 3, 2024

Hi Kim. Thanks for getting back to me. 

I think that’s what I expected, but was really hoping to do it in one API call. Every second counts!

Thanks anyway
Stef