Skip to main content

Hi - hope someone can help. I suspect I'm making a fairly basic error.

I'm attempting to use the klaviyo-api-php library, hosted on GitHub, to add subscribers and update profiles.

I've already successfully used the php library to retrieve info on profiles i.e. using $klaviyo->Lists->getListProfiles($list_id, $fields_profile, $filter);

However I'm now really stuck when trying to subscribe a profile to list.

Error message returned is:
"status":400,"code":"invalid","title":"Invalid input.","detail":"The payload provided in the request is invalid.","source":{"pointer":"/data"},"meta":{}}]}

I can't figure out how to structure the data in the request $body.
I know it needs to be an associative array and have tried various options, but everything I try fails.

Below is my $body payload. (list_id removed)

I'd be really grateful if anyone could help me structure or format it correctly. I can't see where I'm going wrong!

I'd be really grateful for any help. Many thanks


$body = array(
'list_id' => 'LIST_ID',
'profiles' => array(
array(
'email' => 'user@example.com',
'properties' => array(
'$first_name' => 'John',
'$last_name' => 'Doe',
),
),
),
);

$klaviyo->Profiles->subscribeProfiles($body);

GitHub docs:

https://github.com/klaviyo/klaviyo-api-php#user-content-subscribe-profiles

## Positional Arguments

# $body | associative array

$klaviyo->Profiles->subscribeProfiles($body);

Klaviyo API docs:
https://developers.klaviyo.com/en/v2022-10-17/reference/subscribe_profiles

Hi @Stef,

I haven’t used the PHP library so I might not be the most helpful here, but what you have looks correct to me.

The only thing I can suggest is getting rid of the `properties` prop, like this…

$body = array(
'list_id' => 'LIST_ID',
'profiles' => array(
array(
'email' => 'user@example.com',
'$first_name' => 'John',
'$last_name' => 'Doe',
),
),
);

Here is an example JSON body I used just this morning… it’s a slightly different endpoint, but I think it gives you an idea of how it works...

{
"profiles": [
{
"email": "testing@example.comcom",
"sample_property": "a value"
}
]
}

Let me know.

Cheers,

Kevin.


Yeah, the documentation hasn’t quite caught up with the new APIs yet, but I’m 99% sure you’re having the same problem I was with the Node API. The format for the body should be:

 

"data": {
          "type": "profile-subscription-bulk-create-job",
          "attributes": {
               "subscriptions": [
                    {
                         "email": "bob@eample.com"
                    },
                    {
                         "email": "tom@example.com"
                    }
               ],
               "list_id": "12345"
          }
     }

(taken from the second link you posted). Notably

  • the whole thing needs to get wrapped in a “data” (which is different, and internal to the ‘data’ object in the POST request (i.e., the actual POST request will be `data { data: { type ...`)).
  • you need to specify the “type” (the SDK very much makes you think you don’t need to, and you shouldn’t need to, but at least in this iteration, the SDK is just a thin wrapper, so you have to specify everything)
  • It looks like the bit of it that you have all goes inside of “attributes”, and what you have as “profiles” needs to be called “subscriptions”

NB: I’m new to Klaviyo and not using PHP, so the above could be wrong, but … I hope it helps :)


 

Hi @KeviSunshine 

Thanks for replying, I appreciate it 👍

I tried a few more variations based on your suggestions but unfortunately still not successful.

Still getting the same error returned:

"title":"Invalid input.","detail":"The payload provided in the request is invalid.","source":{"pointer":"/data"},"meta":{}}]}

It’s one of those things that I think should be fairly simple but frustratingly I’ve managed to burn most of the day trying to figure it out.

I'd be really grateful if anyone who has successfully used the most recent PHP SDK can share what they’d did or point me in the right direction.

 

 

 

The only thing I can suggest is getting rid of the `properties` prop, like this…

$body = array(
'list_id' => 'LIST_ID',
'profiles' => array(
array(
'email' => 'user@example.com',
'$first_name' => 'John',
'$last_name' => 'Doe',
),
),
);

Here is an example JSON body I used just this morning… it’s a slightly different endpoint, but I think it gives you an idea of how it works...

{
"profiles": [
{
"email": "testing@example.comcom",
"sample_property": "a value"
}
]
}

Let me know.

 

 


I took a look at the documentation for the PHP SDK. The subscribe profiles snippet is linking to this Klaviyo API: https://developers.klaviyo.com/en/reference/subscribe_profiles.

This API looks to be expecting a `subscriptions` property rather than a profile property… Have you tried that? Something like this…

$body = array(
'list_id' => 'LIST_ID',
'subscriptions' => array(
array(
'email' => 'user@example.com'
),
),
);

 


Yea - tried that too… no luck. Thanks again though!

I think I must be missing something fundamental here… Probably to do with how the array is put together. I wondered if it maybe required a JSON style object (like in the API docs) along the lines of:

$body = '{"subscriptions": [{"email": "mail@emailaddress.com"}],"list_id": "LIST_ID"}';

 and array but that seems to fail too. 🙁

stumped!


Hi - Brandon @ Klaviyo support kindly helped me out with this one so thought I’d share an example of a working $body payload.  Obviously LIST_ID needs replacing with your list ID.

Thanks

 

$body = array(
"data" => array(
"type" => "profile-subscription-bulk-create-job",
"attributes" => array(
"subscriptions" => array(
array(
"email" => "user@example.com",
)
),
"list_id" => "LIST_ID"
)
)
);

$klaviyo->Profiles->subscribeProfiles($body);

 


Hi, i just update old script api to new version and found this “$body” structure was confused and not easy to read.

so i found easier way, im using php so i just copy json data structure form API references page and convert to php array, 
example subscribe profile to list  

{"data":{"type":"profile-subscription-bulk-create-job","attributes":{"custom_source":"Marketing Event","profiles":{"data":[{"type":"profile","id":"01GDDKASAP8TKDDA2GRZDSVP4H","attributes":{"email":"matt-kemp@klaviyo-demo.com","phone_number":"+15005550006","subscriptions":{"email":{"marketing":{"consent":"SUBSCRIBED"}},"sms":{"marketing":{"consent":"SUBSCRIBED"}}}}}]}},"relationships":{"list":{"data":{"type":"list","id":"Y6nRLr"}}}}}

convert to php array
 


$body= [
"data" => [
"type" => "profile-subscription-bulk-create-job",
"attributes" => [
"custom_source" => "Marketing Event",
"profiles" => [
"data" => [
[
"type" => "profile",
"id" => "01GDDKASAP8TKDDA2GRZDSVP4H",
"attributes" => [
"email" => "matt-kemp@klaviyo-demo.com",
"phone_number" => "+15005550006",
"subscriptions" => [
"email" => [
"marketing" => ["consent" => "SUBSCRIBED"],
],
"sms" => [
"marketing" => ["consent" => "SUBSCRIBED"],
],
],
],
],
],
],
],
"relationships" => [
"list" => ["data" => ["type" => "list", "id" => "Y6nRLr"]],
],
],
];

its easier to modify params needed and you can pass the params to sdk api call. this params for subscribe email to list

$body= [
"data" => [
"type" => "profile-subscription-bulk-create-job",
"attributes" => [
"custom_source" => "Marketing Event",
"profiles" => [
"data" => [
[
"type" => "profile",
"attributes" => [
"email" => "matt-kemp@klaviyo-demo.com",
],
],
],
],
],
],
"relationships" => [
"list" => ["data" => ["type" => "list", "id" => "Y6nRLr"]],
],
],
];

$klaviyo->Profiles->subscribeProfiles($body);