我在下面有这段代码,可以将用户添加到 Mailchimp 中预先存在的列表中。
$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',
'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');
此代码仅将用户添加到列表中,当我尝试两次添加同一用户的详细信息时,它会在第二次尝试时抛出以下错误:
test@user.com is already a list member. Use PATCH to update existing members.
如何使用 PATCH 更新用户详细信息?我不确定在哪里指定它。
最佳答案
我知道我哪里出错了。当用户最初被添加到列表时,响应会提供一个 ID。我需要将 ID 与那些人的详细信息一起存储在我的数据库中,并在我想更新 Mailchimp 列表中的用户详细信息时调用我正在调用的 url 中引用 ID。
https://us2.api.mailchimp.com/3.0/lists/<list_id_goes_here>/members/<members_id_goes_here>
感谢@TooMuchPete 提供正确的 curl 命令。
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
关于php - 使用 cURL 和 Mailchimp API v3 更新列表中的订阅者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30531798/