我試圖使用MailChimp API將訂閱者添加到列表中,一旦他們檢查了窗體上的框。我可以用cURL來做到這一點,並且它可以正常工作,但是這個服務器不會啓用它。我試圖使用file_get_contents(),但是這給了我一個401錯誤。我檢查了API密鑰和列表ID,它們都是正確的。我猜測我的語法已經搞亂了,或者我把錯誤的地方放在了一些地方,但是我沒有看到這個問題。我試過的是以下。有人有主意嗎?使用MailChimp API沒有cURL
if (isset($_POST['mailtest']) == 'value1'){
$email = $_POST['email'];
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$mailchimp_api_key = 'apikey';
$mailchimp_list_id = 'listid';
$data = array(
'apikey' => $mailchimp_api_key,
'id' => $mailchimp_list_id,
'email' => $email
'status'=>'subscribed'
);
$memberId = md5(strtolower($email));
$dataCenter = substr($mailchimp_api_key ,strpos($mailchimp_api_key, '-')+1);
$endpoint = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $mailchimp_list_id . '/members/' . $memberId;
if ($fname && $lname) $data['merge_vars '] = array('FNAME' => $fname, 'LNAME' => $lname);
$json_data = json_encode($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/json",
"Connection: close\r\n" .
"Content-length: " . strlen($json_data) . "\r\n",
'data' => $json_data,
),);
$context = stream_context_create($options);
$result = file_get_contents($endpoint, false, $context);
}
http 401錯誤意味着未經授權。所以你沒有正確的認證。 – Augwa
如果你想堅持'file_get_contents()',你需要一些上下文來驗證(閱讀手冊)。看看[stream_context_create()](http://php.net/manual/en/function.stream-context-create.php)來創建你需要的頭文件。 – fpietka