gpt4 book ai didi

curl - Mailchimp API 3.0批量订阅

转载 作者:行者123 更新时间:2023-12-02 19:53:54 25 4
gpt4 key购买 nike

根据文档,到/ lists / {list_id}的POST应该批量订阅或取消订阅列表成员。

如果我派两名成员;一名新成员和一名退订成员:

    {
"update_existing":true,
"members":[
{
"email_address":"yyyy@yyy.yy",
"email_type":"html",
"status":"subscribed"
},
{
"email_address":"xxx@xxx.xx",
"email_type":"html",
"status":"subscribed"
}
]
}


文档( http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#create-post_lists_list_id)指出,生成的JSON将包括具有new_members,updated_members的数组和具有错误的成员的数组:


响应体参数

错误:对象数组,每个对象代表无法添加到列表或更新的电子邮件地址,以及提供更多详细信息的错误消息。




但是相反,我得到了HTTP Status 400并显示以下错误:

{
"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error- glossary/",
"title":"Member Exists",
"status":400,
"detail":"xxx@xxx.xx is in a compliance state due to unsubscribe, bounce, or compliance review and cannot be subscribed.",
"instance":""
}

最佳答案

对于Batch subscribe,请尝试以下代码,这是经过测试的100%工作代码。

<?php
$apikey = ''; // Your Mailchimp ApiKey
$list_id = ''; // your List ID Where you want to add subscriber

$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'dada_mail_to_mailchimp';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}

$sql = 'SELECT * FROM emails Limit 2';
$result = $conn->query($sql);
$finalData = [];
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$individulData = array(
'apikey' => $apikey,
'email_address' => $row['email'],
'status' => $row['status'],//subscribe,pending,unsubscribe
'merge_fields' => array(
'FNAME' => $row['FNAME'],
'LNAME' => $row['LNAME'],
)
);

$json_individulData = json_encode($individulData);
$finalData['operations'][] =
array(
"method" => "POST",
"path" => "/lists/$list_id/members/",
"body" => $json_individulData
);
}
}

$api_response = batchSubscribe($finalData, $apikey);
print_r($api_response);
$conn->close();

/**
* Mailchimp API- List Batch Subscribe added function
*
* @param array $data Passed you data as an array format.
* @param string $apikey your mailchimp api key.
*
* @return mixed
*/
function batchSubscribe(array $data, $apikey)
{
$auth = base64_encode('user:' . $apikey);
$json_postData = json_encode($data);
$ch = curl_init();
$dataCenter = substr($apikey, strpos($apikey, '-') + 1);
$curlopt_url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/batches/';
curl_setopt($ch, CURLOPT_URL, $curlopt_url);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic ' . $auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.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_postData);

$result = curl_exec($ch);
return $result;
}

关于curl - Mailchimp API 3.0批量订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40973388/

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