Skip to content

Instantly share code, notes, and snippets.

@zmalter99
Created January 5, 2024 20:09
Show Gist options
  • Select an option

  • Save zmalter99/9346c953704ca43f1be983db541a27f5 to your computer and use it in GitHub Desktop.

Select an option

Save zmalter99/9346c953704ca43f1be983db541a27f5 to your computer and use it in GitHub Desktop.
PHP Klaviyo Opt In
<?php
/************************* Klaviyo Email Signup *************************/
class Klaviyo
{
public static function subscribeToMarketing($email, $listID, $accessToken)
{
$curl = new \Curl\Curl();
$curl->setHeader('Authorization', "Klaviyo-API-Key {$accessToken}");
$curl->setHeader('Accept', 'application/json');
$curl->setHeader('Content-Type', 'application/json');
$curl->setHeader('revision', '2023-02-22');
$data = [
"data" => [
"type" => "profile-subscription-bulk-create-job",
"attributes" => [
"list_id" => $listID,
"subscriptions" => [
[
"channels" => [
"email" => ["MARKETING"],
],
"email" => $email,
]
]
]
]
];
$curl->post("https://a.klaviyo.com/api/profile-subscription-bulk-create-jobs/", $data);
}
public static function subscribeToMarketingSMS($phone, $listID, $accessToken)
{
$curl = new \Curl\Curl();
$curl->setHeader('Authorization', "Klaviyo-API-Key {$accessToken}");
$curl->setHeader('Accept', 'application/json');
$curl->setHeader('Content-Type', 'application/json');
$curl->setHeader('revision', '2023-02-22');
$data = [
"data" => [
"type" => "profile-subscription-bulk-create-job",
"attributes" => [
"list_id" => $listID,
"subscriptions" => [
[
"channels" => [
"sms" => ["MARKETING"],
],
"phone_number" => str_replace([' ', '-', '(', ')'], '', $phone),
]
]
]
]
];
$curl->post("https://a.klaviyo.com/api/profile-subscription-bulk-create-jobs/", $data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment