Created
January 5, 2024 20:09
-
-
Save zmalter99/9346c953704ca43f1be983db541a27f5 to your computer and use it in GitHub Desktop.
PHP Klaviyo Opt In
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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