【问题标题】:I hit this error while integrating klaviyo with laravel我在将 klaviyo 与 laravel 集成时遇到了这个错误
【发布时间】:2021-10-26 21:30:31
【问题描述】:

我正在使用this package As SDK。所以基本上我想要实现的是将用户添加到列表中,但具有同意状态或电子邮件和短信,我从我自己的问题的一个答案中找到的是这段代码。所以我尝试了它,但它又遇到了另一个错误。

TypeError
Cannot access offset of type string on string

代码来自@Ohgodwhy 的answer.

public function chnages()
    {
        $client = new Klaviyo('Your_Private_apikey', 'Public_Api_key');
        $customer_properties =  [
            '$email' => "someone@mailinator9.com",
            '$first_name' => "Thomas9",
            '$last_name' => "Jefferson",
            'phone_number' => "1234567890"
        ];
        $consents = [];
        if (request()->get('sms_consent', false)) {
            $consents['sms_consent'] = true;
        }
        if (request()->get('email_consent', false)) {
            $consents['email_consent'] = true;
        }
        foreach ($consents as $type => $consented) {
            if ($consented) {
                $consents['$consent'] = array_merge(
                    data_get($consents, '$consent', []),
                    [explode('_', $type)[0]] //e.g. sms, email
                );
            }
        }

        $client->lists->addSubscribersToList('LIST_ID', array_merge($customer_properties, $consents));
    }

【问题讨论】:

    标签: php arrays laravel string api


    【解决方案1】:

    $consents['$consent'] 这不起作用,数组值可以是变量或字符串,但不能是字符串内的变量。此外,您的 foreach 中没有 $consent,要检查您需要执行的 $consents 键值$consents[$type] data_get($consents, '$consent', []) 也一样,它必须是 data_get($consents, $consented, [])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      相关资源
      最近更新 更多