2016-07-15 96 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));

相關問題