Skip to main content

I need to integrate the klaviyo API with an existing legacy php system such that when a customer successfully signs up for a service they are automatically added to a list in Klaviyo. I cannot include a signup form as would be normal because all of this is done server-side in the background.

I made a simple program for testing purposes using the PHP SDK but I am getting a response I do not understand. I am hoping someone can tell me where I am going wrong on this.

The program code is fairly simple:

<?php
require_once './includes/klaviyo_php_sdk_2.2.5.0/vendor/autoload.php';

use Klaviyo\Klaviyo as Klaviyo;

$addProfile = array(
array(
'email' => "dan@divinusinc.com",
'first_name' => "Test",
'last_name' => "Man",
'phone_number' => "(214) 555-1212",
'address' => "23 Skidoo Street",
'address_2' => "Dark Side of the Moon",
'city' => "Dallas",
'state' => "TX",
'country' => "USA",
'zip_code' => "75216",
'plan_type' => "Some-Random-Plan",
'start_date' => "03-21-2021",
'renewal_date' => "03-21-2022"
)
);

$client = new Klaviyo( 'MY_PUBLIC_KEY GOES HERE', 'my_private_key_goes_here' );

#Add members to list without affecting consent status
$client->lists->addMembersToList( 'My_List_ID_Goes_Here', $addProfile );

?>

but I am getting the following errors I do not understand;

[root@ip-172-31-3-105 Enroll]# php ./xCodeBlock.php
PHP Notice: Undefined index: $email in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php on line 454

Notice: Undefined index: $email in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php on line 454
PHP Fatal error: Uncaught Klaviyo\Exception\KlaviyoException: is not an instance of ProfileModel, You must identify the person by their email, using a $email key, or a unique identifier, using a $id. in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php:453
Stack trace:
#0 /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/Lists.php(225): Klaviyo\KlaviyoAPI->checkProfile(Array)
#1 /sites/dev/Enroll/xCodeBlock.php(27): Klaviyo\Lists->addMembersToList('UJLqnA', Array)
#2 {main}
thrown in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php on line 453

Fatal error: Uncaught Klaviyo\Exception\KlaviyoException: is not an instance of ProfileModel, You must identify the person by their email, using a $email key, or a unique identifier, using a $id. in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php:453
Stack trace:
#0 /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/Lists.php(225): Klaviyo\KlaviyoAPI->checkProfile(Array)
#1 /sites/dev/Enroll/xCodeBlock.php(27): Klaviyo\Lists->addMembersToList('UJLqnA', Array)
#2 {main}
thrown in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php on line 453
[root@ip-172-31-3-105 Enroll]#

I am using root level access and it is apparent the program is finding the API source, but I just do not know where/why this is failing.

Any help would be appreciated.

Dan Davis

Hi @Divinus Inc 

 

The addMembersToList method accepts an array of KlaviyoProfile objects. When you define your array of profiles to add to the list you can follow the example in our PHP SDK docs here (the example refers to the identify call but the pattern is the same): https://github.com/klaviyo/php-klaviyo#or-just-add-a-property-to-someone

Best,

Scott


@Scott ,

 

Thanks for the insight; I modified the code to this:

<?php
require_once './includes/klaviyo_php_sdk_2.2.5.0/vendor/autoload.php';

use Klaviyo\Klaviyo as Klaviyo;
use Klaviyo\Model\ProfileModel as KlaviyoProfile;

$client = new Klaviyo( 'MY_PUBLIC_KEY GOES HERE', 'my_private_key_goes_here' );

$addProfile = new KlaviyoProfile(
array(
'$email' => "dan@divinusinc.com",
'$first_name' => "Test",
'$last_name' => "Man",
'$phone_number' => "(214) 555-1212",
// '$address' => "23 Skidoo Street",
// '$address_2' => "Dark side of the Moon",
// '$city' => "Dallas",
// '$state' => "TX",
// '$country' => "USA",
// '$zip_code' => "75216",
// 'plan_type' => "Some-Random-Plan",
// 'start_date' => "03-21-2021",
// 'renewal_date' => "03-21-2022"
)
);

#Add members to list without affecting consent status
$client->lists->addMembersToList( 'My_List_ID_Goes_Here', $addProfile );

?>

But I am still getting errors:

>root@ip-172-31-3-105 Enroll]# php ./xCodeBlock.php
PHP Fatal error: Uncaught Klaviyo\Exception\KlaviyoException: is not an instance of ProfileModel, You must identify the person by their email, using a $email key, or a unique identifier, using a $id. in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php:453
Stack trace:
#0 /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/Lists.php(225): Klaviyo\KlaviyoAPI->checkProfile(Object(Klaviyo\Model\ProfileModel))
#1 /sites/dev/Enroll/xCodeBlock.php(29): Klaviyo\Lists->addMembersToList('UJLqnA', Object(Klaviyo\Model\ProfileModel))
#2 {main}
thrown in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php on line 453

Fatal error: Uncaught Klaviyo\Exception\KlaviyoException: is not an instance of ProfileModel, You must identify the person by their email, using a $email key, or a unique identifier, using a $id. in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php:453
Stack trace:
#0 /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/Lists.php(225): Klaviyo\KlaviyoAPI->checkProfile(Object(Klaviyo\Model\ProfileModel))
#1 /sites/dev/Enroll/xCodeBlock.php(29): Klaviyo\Lists->addMembersToList('UJLqnA', Object(Klaviyo\Model\ProfileModel))
#2 {main}
thrown in /sites/dev/Enroll/includes/klaviyo_php_sdk_2.2.5.0/vendor/klaviyo/php-sdk/src/KlaviyoAPI.php on line 453
>root@ip-172-31-3-105 Enroll]#

Any thoughts on this?

Dan


Hi Dan,

 

Yes, you’re nearly there, just wrap the KlaviyoProfile in an array e.g.

$addProfile = array(
new KlaviyoProfile(
array(
'$email' => "dan@divinusinc.com",
'$first_name' => "Test",
'$last_name' => "Man",
'$phone_number' => "(214) 555-1212",
)
)
);

FYI, you can add multiple profiles with a single request, just add other KlaviyoProfiles to the array you pass to the addMembersToList method.

 

Best,

Scott


Okay, I guess I am just dumb, but  I used the exact syntax from the SDK Docs you referenced in the first reply:

use Klaviyo\Model\ProfileModel as KlaviyoProfile;

$profile = new KlaviyoProfile(
array(
'$email' => 'thomas.jefferson@mailinator.com',
'$first_name' => 'Thomas',
'$last_name' => 'Jefferson',
'Plan' => 'Premium'
)
);

$client->publicAPI->identify( $profile );

I made the change you suggested but that causes PHP syntax errors:

>root@ip-172-31-3-105 Enroll]# php ./xCodeBlock.php
PHP Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in /sites/dev/Enroll/xCodeBlock.php on line 11

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in /sites/dev/Enroll/xCodeBlock.php on line 11
>root@ip-172-31-3-105 Enroll]#

 


Okay, I figured this out. The correct syntax is:

$addProfile = array(
new KlaviyoProfile(
array(
'$email' => "dan@divinusinc.com",
'$first_name' => "Test",
'$last_name' => "Man",
'$phone_number' => "(214) 555-1212",
'$address' => "23 Skidoo Street",
'$address_2' => "Dark side of the Moon",
'$city' => "Dallas",
'$state' => "TX",
'$country' => "USA",
'$zip_code' => "75216",
'plan_type' => "Some-Random-Plan",
'start_date' => "03-21-2021",
'renewal_date' => "03-21-2022"
)
)
);

 

So, this is my final question on the syntax. The address information is being added to the profile as custom properties, how do I get the upload to populate the Klaviyo standard properties instead? Or is that even possible?


Hi Dan,

 

Apologies for any confusion. Yes, the ProfileModel (imported as KlaviyoProfile in the documentation) accepts an associative array with the profile properties. I’ll update my response which contained the typo.

With regard to the address fields, these are “special properties” in Klaviyo. The address-specific special properties are:

'$address1', '$address2', '$city', '$region', '$zip', '$country'

 

Best,

Scott


@Scott ,

Thanks for all of your help, it is appreciated.

 

Dan


@Scott Can we add consent array in this ,for sms and email?


Hi @MeetAmin,

Thank you for sharing your comment here.

For addMembersToList, there is no “consent” involved in the sense that a user will not be subscribed by virtue of including this field.  The property is primarily used for segmentation purposes (used for GDPR) i.e. Properties about someone > $consent contains X.  It would be helpful if you could elaborate further on your end-goal in a separate thread and we can discuss there!


Reply