php – 使用cURL和Mailchimp API v3更新列表中的订阅者
作者:互联网
我在下面的代码中将用户添加到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");
标签:mailchimp-api-v3-0,php,mailchimp,curl 来源: https://codeday.me/bug/20191007/1869531.html