无法检查klaviyo laravel软件包中SMS的同意状态

afdcj2ne  于 2022-11-18  发布在  其他
关注(0)|答案(3)|浏览(108)

我已经尝试将klaviyo与laravel与这段代码集成,但它只添加了同意电子邮件(绿色复选框旁边的电子邮件在klaviyo),但它也应该添加同意短信相同的方式为电子邮件.我做错了什么?到目前为止,我已经尝试了这个和this,但击中了一个不同的错误.

public function chnages()
    {
        $client = new Klaviyo('Private_Api_key', 'Public_key');
        $profile = new KlaviyoProfile(
            array(
                '$email' => 'thomas1.jefferson@mailinator.com',
                '$first_name' => 'Thomas11',
                '$last_name' => 'Jefferson',
                '$consent' => ['sms','email'],
                '$sms_consent' => true,
                '$phone_number' => "1234567890",
                'Plan' => 'Premium'
            )
        );
        // dd($profile);
        $client->lists->addSubscribersToList('Klaviyo_list_id', array($profile));
    }
db2dz4w8

db2dz4w81#

这里是我为laravel编写的正确代码,基本上,您需要将请求从表单发送到该函数,然后从这里跟踪此代码和API文档。***注意:短信同意只能通过从订阅端点,所以已经有我提到的URL。***请随时询问和建议这个线程。

public function forform(Request $request)
    {
        $ifsmscheckd = $request->input('sms_consent') ? true : false; //if input is there then its true
        if ($ifsmscheckd) {
            // if both checkd
            $curl = curl_init();
            curl_setopt_array($curl, [
                CURLOPT_URL => "https://a.klaviyo.com/api/v2/list/{LIST_ID}/subscribe?api_key=PRIVATE_KEY",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => json_encode([
                    'profiles' => [
                        [
                            'email' => $request->input('name'),
                            'phone_number' => $request->input('phone_number'),
                            'sms_consent' => $request->input('sms_consent') ? true : false,
                            '$consent' => ['sms', 'email']
                        ],
                    ],
                ]),
                CURLOPT_HTTPHEADER => [
                    "Accept: application/json",
                    "Content-Type: application/json"
                ],
            ]);

            $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);
            if ($err) {
                echo "cURL Error #:" . $err . "cmdskcmdsl";
            } else {
                echo $response;
            }
        }
        else{
            return;
        }
    }
wsewodh2

wsewodh23#

我发现下面的作品使用https://github.com/klaviyo/php-klaviyo。这个软件包是过时的,但它是我们正在使用的时刻。对不起,不能更新软件包在这个时候。

public function subscribeMemberToSMSList($list, $phonenumber){
        $NUM_OF_ATTEMPTS = 5;
        $attempts = 0;            
        $profile = new KlaviyoProfile(array('$phone_number'=>$phonenumber, 'phone_number' => $phonenumber, 'sms_consent' => true));              
        $arrayOfProfiles = array($profile);
        
        do {
            try{                     
                $listresponse = $this->client->lists->subscribeMembersToList($list, $arrayOfProfiles);                    
                return 'Success';                    
            } catch (Exception $e)
            {                        
                    $attempts++;
                    $pos = strpos($e->getMessage(), 'Request was throttled.');                
                    if ($pos === false) {                                             
                        return $e->getMessage();
                    } else {
                        $int = (int) filter_var($e->getMessage(), FILTER_SANITIZE_NUMBER_INT);
                        if($int < 10){
                            sleep($int);
                        } else {
                            sleep(5);
                        }
                        continue;
                    }                        
            }               
        } while($attempts < $NUM_OF_ATTEMPTS);
    }

相关问题