我在将 klaviyo 与 laravel 集成时遇到了这个错误 [英] I hit this error while integrating klaviyo with laravel
问题描述
我正在使用 这个包作为SDK.所以基本上我想要实现的是将用户添加到列表中,但具有同意状态或电子邮件和短信,我从我自己的问题的一个答案中找到的是这段代码.所以我尝试了一下,但后来又遇到了另一个错误.
I'm using this package As SDK. So basically what I'm trying to achieve is to add the users to a list but with the consent status or email and SMS, What I found from one answer from my own question is this code. So I tried it but then it hit another error.
TypeError
Cannot access offset of type string on string
来自@Ohgodwhy 的代码 答案.
Code from @Ohgodwhy's 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));
}
推荐答案
$consents['$consent']
这行不通,数组值可以是变量或字符串,但是不是字符串内的变量.此外,您的 foreach 中没有 $consent,要检查您需要执行的 $consents 键值 $consents[$type]
对于 data_get($consents, '$consent', [])
来说同样重要,它需要 data_get($consents, $consents, [])
$consents['$consent']
this is not going to work, array values can be either a variable OR a string but not a variable inside a string. Also, you don’t have $consent in your foreach, to check the $consents key value you need to do $consents[$type]
The same thing counts for data_get($consents, '$consent', [])
which need to be data_get($consents, $consented, [])
这篇关于我在将 klaviyo 与 laravel 集成时遇到了这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!