2017-02-18 88 views
0

我試圖使用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); 
    } 
+0

http 401錯誤意味着未經授權。所以你沒有正確的認證。 – Augwa

+0

如果你想堅持'file_get_contents()',你需要一些上下文來驗證(閱讀手冊)。看看[stream_context_create()](http://php.net/manual/en/function.stream-context-create.php)來創建你需要的頭文件。 – fpietka

回答

0

您需要設置基本身份驗證標頭。同樣,你似乎將你的dataCentre包含在你的API密鑰中,所以下面的代碼可能需要一些調整,因爲我實際上不知道$mailchimp_api_key設置爲什麼。

// this is improper usage of isset. isset returns a boolean 
// since you're not doing a strict match it'll always work, 
// unless mailtest is null or isn't set. 
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' => implode(
       "\r\n", 
       [ 
        "Content-type: application/json", 
        "Content-length: " . strlen($json_data), 
        "Authorization: Basic " . base64_encode("anystring:$mailchimp_api_key") // see note above 
       ] 
      ), 
      'content' => $json_data, 
     ) 
    ); 
    $context = stream_context_create($options); 
    $result = file_get_contents($endpoint, false, $context); 
} 
+0

$ mailchimp_api_key設置爲字母數字鍵,後跟-usX數字。這段代碼沒有給出401錯誤,所以我猜想它的驗證,但它給出了一個404錯誤。生成的URL與使用cURL時生成的URL相同,因此我不確定它在尋找什麼,也沒有找到。 – Ezra

+0

它確實需要一些小小的調整,但是在我將POST更改爲PUT並將「email」更改爲「email_address」後,它起作用。 – Ezra