gpt4 book ai didi

php - 使用 Mailchimp 的 API v3 将订阅者添加到列表

转载 作者:IT老高 更新时间:2023-10-28 12:09:09 27 4
gpt4 key购买 nike

我正在尝试将用户添加到我在 Mailchimp 中创建的列表中,但我在任何地方都找不到任何代码示例。我已经尝试弄清楚如何使用 API,但我是一个非常喜欢“看例子然后学习”的人。

我尝试过使用 API 的第 2 版,但尽管从网上的示例开始工作,但似乎没有任何效果,Mailchimp 在其网站上对早期版本的 API 进行了以下说明:

Versions 2.0 and earlier are deprecated. Only minimal support—bug fixes, security patches—will be available for those versions.

更新 1:我根据 TooMuchPete 的 answer 关于管理订阅者的链接做了一些进一步的研究,并更改了我找到的一些代码 here ,但它不起作用,因为函数 http_build_query() 没有t 处理嵌套数组。我不确定如何处理添加订阅者的“merge_fields”部分。我当前的代码如下:

$postdata = http_build_query(
array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
)
);

$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);

$context = stream_context_create($opts);

$result = file_get_contents('https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/', false, $context);

var_dump($result);
die('Mailchimp executed');

更新 2:我现在求助于使用 curl 并且我已经设法得到了一些几乎可以工作的东西。数据发送到 Mailchimp,但我收到错误 “您的请求不包含 API key 。” 我猜我需要按照 here 所述进行身份验证。我已经尝试将它添加到没有工作的 http header 中。请参阅下面的代码:

$apikey = '<api_key>';
$auth = base64_encode( 'user:'.$apikey );

$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
);
$json_data = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json/r/n
Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

$result = curl_exec($ch);

var_dump($result);
die('Mailchimp executed');

最佳答案

基于 List Members Instance docs ,最简单的方法是使用 PUT 请求,根据文档 “添加新的列表成员或更新成员,如果电子邮件已存在于列表”

此外,apikey 绝对不是 json schema 的一部分并且将其包含在您的 json 请求中是没有意义的。

此外,正如@TooMuchPete 的评论中所述,您可以使用 CURLOPT_USERPWD 进行基本 http 身份验证,如下所示。

我正在使用以下函数来添加和更新列表成员。根据您的列表参数,您可能需要包含一组略有不同的 merge_fields

$data = [
'email' => 'johndoe@example.com',
'status' => 'subscribed',
'firstname' => 'john',
'lastname' => 'doe'
];

syncMailchimp($data);

function syncMailchimp($data) {
$apiKey = 'your api key';
$listId = 'your list id';

$memberId = md5(strtolower($data['email']));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;

$json = json_encode([
'email_address' => $data['email'],
'status' => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => [
'FNAME' => $data['firstname'],
'LNAME' => $data['lastname']
]
]);

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['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);

return $httpCode;
}

关于php - 使用 Mailchimp 的 API v3 将订阅者添加到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30481979/

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