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