gpt4 book ai didi

php - 无法使用 MailChimp API 3.0 将用户添加到组

转载 作者:可可西里 更新时间:2023-11-01 12:53:09 25 4
gpt4 key购买 nike

我正在尝试更新我的代码,以便新用户订阅我的 MailChimp 时事通讯。

我希望它能将用户添加到一个组中,这样我就可以在发送时事通讯时区分用户。但是,此代码订阅了用户但未将其添加到任何组。我究竟做错了什么?我正在使用 MailChimp API 3.0。

我的代码:

$apiKey     = 'XXXX';
$list_id = 'XXXX';
$memberId = md5(strtolower($data['email']));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);

$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . $memberId;

$json = json_encode(array(
'email_address' => $data['email'],
'status' => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => array(
'FNAME' => $data['firstname'],
'LNAME' => $data['lastname'],
'GROUPINGS' => array(
0 => array(
'name' => 'Interests',
'groups' => array( $data['tag'] ),
),
),
),
));

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

最佳答案

您在错误的数组中传递兴趣分组。您需要将兴趣组 ID 传递到 interests 数组中,值为 true

$json = json_encode(array(
'email_address' => 'test@example.com',
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'FirstName',
'LNAME' => 'LastName',
),
'interests' => array(
'9143cf3bd1' => true,
'789cf3bds1' => true
),
));

您将通过请求此 URL 获得列表的兴趣组的 id [9143cf3bd1,789cf3bds1 in above example]

/lists/{list_id}/interest-categories

see doc here

如果要从组中删除订阅者,需要在更新请求中将其值设置为false

关于php - 无法使用 MailChimp API 3.0 将用户添加到组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38613273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com