根据“ 列表成员实例”文档,最简单的方法是使用一个 PUT
请求,该请求根据文档
“添加新的列表成员,或者如果列表中已经存在电子邮件,则更新该成员” 。
此外
apikey,绝对不是json模式的一部分,将它包含在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_enpre([ '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;}


