2012-10-19 80 views
3

我正在尋找一些幫助,找出爲什麼這個基本腳本不適合我。我試圖使用MailChimp API來定製表單將新訂戶添加到列表中。腳本沒有返回任何東西。需要基本的MailChimp API幫助

我知道我正確地使用了API,因爲當我直接在瀏覽器中輸入url時,該對象在MailChimp中訂閱,這讓我覺得curl沒有設置正確。有什麼想法嗎?

<?php 
$apikey = 'xxx'; 
$listID = 'yyy'; 

$email = htmlspecialchars(stripslashes(trim($_POST['EMAIL']))); 
$fname = htmlspecialchars(stripslashes(trim($_POST['FNAME']))); 
$lname = htmlspecialchars(stripslashes(trim($_POST['LNAME']))); 

     if (!empty($_POST['EMAIL_UPDATES'])) { 
      $url = sprintf('http://us6.api.mailchimp.com/1.3/?method=listSubscribe&apikey=%s&id=%s&email_address=%s&merge_vars[OPTINIP]=%s&merge_vars[FNAME]=%s&merge_vars[LNAME]=%s&merge_vars[ZIP]=%s&output=json', $apikey, $listID, $email, $_SERVER['REMOTE_ADDR'], $fname, $lname); 
      $ch = curl_init($url); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      $data = curl_exec($ch); 
      curl_close($ch); 
      $arr = json_decode($data, true); 
      if ($arr == 1) { 
       echo 'Check your e-mail and confirm your subscription.'; 
      } else { 
       switch ($arr['code']) { 
        case 214: 
        echo 'You are already subscribed.'; 
        break; 
        // check the MailChimp API for more options 
        default: 
        echo 'Unknown error.'; 
        break;   
       } 
      } 
     } 
?> 
+0

在關閉句柄之前使用'curl_error($ ch)'檢查捲曲錯誤。 – Jrod

回答

1

它看起來像我只是有一些愚蠢的語法錯誤,我已經混合了GET和POST。現在修復。如果有人感興趣,下面是將自定義「更新配置文件」表單與多個MailChimp列表(本例中爲2個列表)同步的代碼。如果任何人有建議使代碼更有效率,那也是非常好的。

<?php 
$apikey = 'xxx'; 
$listID1 = 'yyy'; 
$listID2 = 'zzz'; 

$email = htmlspecialchars(stripslashes(trim($_POST['EMAIL']))); 
$fname = htmlspecialchars(stripslashes(trim($_POST['FNAME']))); 
$lname = htmlspecialchars(stripslashes(trim($_POST['LNAME']))); 

     $mh = curl_multi_init(); 

     if ($_POST['EMAILUPDATES'] == 'Yes') { 
      $url1 = 'http://us6.api.mailchimp.com/1.3/?method=listSubscribe&apikey='.$apikey.'&id='.$listID1.'&email_address='.$email.'&merge_vars[OPTINIP]='.$_SERVER['REMOTE_ADDR'].'&merge_vars[FNAME]='.$fname.'&merge_vars[LNAME]='.$lname.'&update_existing=true'; 
      $ch1 = curl_init($url1); 
      curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); 
      curl_multi_add_handle($mh,$ch1); 

     } 
     else if ($_POST['EMAILUPDATES'] == 'No') { 
      $url3 = 'http://us6.api.mailchimp.com/1.3/?method=listUnsubscribe&apikey='.$apikey.'&id='.$listID1.'&email_address='.$email; 
      $ch3 = curl_init($url3); 
      curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1); 
      curl_multi_add_handle($mh,$ch3); 
     } 
     if ($_POST['BLOGUPDATES'] == 'Yes') { 
      $url2 = 'http://us6.api.mailchimp.com/1.3/?method=listSubscribe&apikey='.$apikey.'&id='.$listID2.'&email_address='.$email.'&merge_vars[OPTINIP]='.$_SERVER['REMOTE_ADDR'].'&merge_vars[FNAME]='.$fname.'&merge_vars[LNAME]='.$lname.'&update_existing=true'; 
      $ch2 = curl_init($url2); 
      curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1); 
      curl_multi_add_handle($mh,$ch2); 
     } 
     else if ($_POST['BLOGUPDATES'] == 'No') { 
      $url4 = 'http://us6.api.mailchimp.com/1.3/?method=listUnsubscribe&apikey='.$apikey.'&id='.$listID2.'&email_address='.$email; 
      $ch4 = curl_init($url4); 
      curl_setopt($ch4, CURLOPT_RETURNTRANSFER, 1); 
      curl_multi_add_handle($mh,$ch4); 
     } 

     $active = null; 
     //execute the handles 
     do { 
      $mrc = curl_multi_exec($mh, $active); 
     } while ($mrc == CURLM_CALL_MULTI_PERFORM); 

     while ($active && $mrc == CURLM_OK) { 
      if (curl_multi_select($mh) != -1) { 
       do { 
        $mrc = curl_multi_exec($mh, $active); 
       } while ($mrc == CURLM_CALL_MULTI_PERFORM); 
      } 
     } 
     if ($_POST['EMAILUPDATES'] == 'Yes') { 
     curl_multi_remove_handle($mh, $ch1); 
     } 
     else if ($_POST['EMAILUPDATES'] == 'No') { 
     curl_multi_remove_handle($mh, $ch3); 
     } 
     if ($_POST['BLOGUPDATES'] == 'Yes') { 
     curl_multi_remove_handle($mh, $ch2); 
     } 
     else if ($_POST['BLOGUPDATES'] == 'No') { 
     curl_multi_remove_handle($mh, $ch4); 
     } 
     curl_multi_close($mh); 
?>