2016-01-22 41 views
0

這裏是我的代碼,我用它來訂閱用戶到多個Mailchimp列表,他們選擇並通過ajax請求發佈到這個文件。這裏是代碼:Mailchimp錯誤肯定多個列表訂閱

因此,由於某種原因,這從Mailchimp返回成功,但它並沒有實際訂閱我輸入的列表。我已確認列表ID是正確的,字段也是如此。

(我已刪除了所有參考實際的ID)

<?php 

    include 'MailChimp.php'; 

    $mailchimp = new MailChimp('MYAPI_KEY'); //I have my code in here 

    $newsletter_keyarray = array(
     "List one" => "listoneid", 
     "List two" => "listtwoid", 
     "List three" => "listthreeid", 
     "List four" => "listfourid" 
    ); 

    $any_errors = false; 

    foreach ($_POST['lists'] as $key => $list) { 
     if(in_array($list, $newsletter_keyarray)){ 

      $merge_vars_array = array(
       'EMAIL' => $_POST['email'] 
      ); 

      if($list == "List one"){ 
       $merge_vars_array['MMERGE1'] = $_POST['fname']; 
       $merge_vars_array['MMERGE2'] = $_POST['sname']; 
       $merge_vars_array['MMERGE3'] = $_POST['org']; 
      }else if($list == "List two"){ 
       $merge_vars_array['FNAME'] = $_POST['fname']; 
       $merge_vars_array['LNAME'] = $_POST['sname']; 
       $merge_vars_array['MMERGE4'] = $_POST['job']; 
       $merge_vars_array['MMERGE5'] = $_POST['org']; 
      }else if($list == "List three"){ 
       $merge_vars_array['FNAME'] = $_POST['fname']; 
       $merge_vars_array['LNAME'] = $_POST['sname']; 
       $merge_vars_array['MMERGE3'] = $_POST['org']; 
       $merge_vars_array['MMERGE4'] = $_POST['job']; 
      }else if($list == "List four"){ 
       $merge_vars_array['FNAME'] = $_POST['fname']; 
       $merge_vars_array['LNAME'] = $_POST['sname']; 
       $merge_vars_array['MMERGE4'] = $_POST['job']; 
       $merge_vars_array['MMERGE5'] = $_POST['org']; 
      } 

      $mailResults = $mailchimp->call('lists/subscribe', array(
       'id' => $newsletter_keyarray[$list],   
       'email' => array('email' => $_POST['email']), 
       'merge_vars' => $merge_vars_array, 
       'double_optin' => false 
      )); 
      if(isset($mailResults['status']) && $mailResults['status'] == 'error'){ 
       $any_errors = true; 
      } 
     } 
    } 

    //enter api and userdetails here 
    $response_array = array(); 

    if($any_errors){ 
     header('Content-type: application/json'); 
     $response_array['status'] = 'error'; 
     if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ 
      $response_array['message'] = 'The email you have entered is not valid'; 
     }else if(isset($mailResults['error']) && $mailResults['error'] != ""){ 
      $response_array['message'] = $mailResults['error']; 
     } 
     echo json_encode($response_array); 
    }else{ 
     header('Content-type: application/json'); 
     $response_array['status'] = 'success'; 
     echo json_encode($response_array); 
    } 

?> 
+0

你爲什麼首先驗證電子郵件? –

回答

0

我已經想通了那種愚蠢的我。

問題是in_array比較值而不是關鍵。所以最後我用array_key_exists

相關問題