2016-07-15 102 views
0

我正尝试使用MailChimp API 3.0将邮件地址添加到使用以下代码的现有列表中,但它一直抛出此错误消息。请谁能告诉我我的代码有什么问题?如何使用API​​将邮件地址添加到MailChimp列表

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

试试这个:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", 'Authorization: Basic ' . $auth));

相关问题