我正在尝试使用MailChimp API 3.0使用以下代码向现有列表添加电子邮件地址,但它不断抛出此错误消息。请问有人能告诉我我的代码有什么问题吗?
401:您的API密钥可能无效,或者您尝试访问该密钥 错误的数据中心。
代码:
$apikey = "xxxxxxxxxxxxxxxxxxxxxxxx-us9";
$list_id = "xxxxxxxxxx";
$MD5_hashed_email_address = md5(strtolower($to_email));
$auth = base64_encode("user:$apikey");
$data = array(
"apikey" => $apikey,
"email_address" => $to_email,
"status" => "subscribed",
"merge_fields" => array(
"FNAME" => $firstname,
"LNAME" => $surname
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://us9.api.mailchimp.com/3.0/lists/$list_id/members/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json",
'Authorization: apikey ' . $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);
答案 0 :(得分:0)
试试这个:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json",
'Authorization: Basic ' . $auth));