2017-02-18 10 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

基本認証ヘッダーを設定する必要があります。同様に、あなたのapiキーでdataCentreをインクルードしているようですので、$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を使用したときに生成されたものと同じですので、探しているものと見つけていないものがわかりません。 – Ezra

+0

マイナーな調整が必要でしたが、POSTをPUTに変更して 'email_address'に変更した後に機能しました。 – Ezra

関連する問題