2014-08-29 186 views
5

我嘗試過使用curl向MailChimp發送數據,但無法獲取要在MailChimp中保存的數據。任何幫助,將不勝感激!無法讓MailChimp API Curl正常工作

這裏是我的代碼:

$mailChimpUrl = "http://us2.api.mailchimp.com/1.3/?method=listSubscribe"; 
$merges = array('FNAME'=>'Dave', 
       'LNAME'=>'Gilmour', 
       'BUILDING'=>'Central High School', 
       'MMERGE17' => '35904', 
       'MMERGE12'=>'Yes' 
       ); 

$apikey="myrealapiishere-us2"; 
$listId="myrealformidishere"; 
$email="[email protected]"; 
$double_optin=true; 
$update_existing=false; 
$replace_interests=true; 
$send_welcome=false; 
$email_type = 'html';    
$data = array(
     'email_address'=>$email, 
     'apikey'=>$apikey, 
     'merge_vars' => $merges, 
     'id' => $listId, 
     'double_optin' => $double_optin, 
     'update_existing' => $update_existing, 
     'replace_interests' => $replace_interests, 
     'send_welcome' => $send_welcome, 
     'email_type' => $email_type 
    ); 


     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $mailChimpUrl); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));  
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
     $result = curl_exec($ch); 
     curl_close($ch); 
+0

什麼呢mailchimp回報呢? – 2014-08-29 02:46:50

+0

它返回true – MagentoMan 2014-08-29 02:50:22

+0

首先,我會建議您使用最新的2.0 API ...排序相同的結構,但它們簡化了一些事情。 – Oberst 2014-08-29 03:03:13

回答

5

正如我在我的評論中提到,你應該考慮最新的2.0 API。除此之外,這是我在生產環境中使用的代碼。

儘管雜亂,它功能。只需用你的東西替換merge_vars和變量 所有的$lead變量都被拉到腳本的其他地方......與此無關。你仍然應該能夠明白這個主意。 ;)

如果還有東西沒有被保存,那麼你在某個地方有一個錯字。檢查一切。花了一個小時才知道我拼錯了'merge_vars'

$merge_vars=array(
    'OPTIN_IP'=>$ip, // Use their IP (if avail) 
    'OPTIN-TIME'=>"now", // Must be something readable by strtotime... 
    'FNAME'=>ucwords(strtolower(trim($lead['first_name']))), 
    'LNAME'=>ucwords(strtolower(trim($lead['last_name']))), 
    'COMPANY'=>ucwords(strtolower(trim($lead['company']))), 
    'ORGTYPE'=>ucwords(strtolower(trim($lead['company_type']))), 
    'PLANNING'=>strtolower(trim(empty($lead['planning_stage'])?"Unknown":$lead['planning_stage'])), 
    ); 

$send_data=array(
    'email'=>array('email'=>$lead['email']), 
    'apikey'=>"", // Your Key 
    'id'=>"", // Your proper List ID 
    'merge_vars'=>$merge_vars, 
    'double_optin'=>false, 
    'update_existing'=>true, 
    'replace_interests'=>false, 
    'send_welcome'=>false, 
    'email_type'=>"html", 
); 

$payload=json_encode($send_data); 
$submit_url="https://us4.api.mailchimp.com/2.0/lists/subscribe.json"; 
$ch=curl_init(); 
curl_setopt($ch,CURLOPT_URL,$submit_url); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch,CURLOPT_POST,true); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$payload); 
$result=curl_exec($ch); 
curl_close($ch); 
$mcdata=json_decode($result); 

if (!empty($mcdata->error)) return "Mailchimp Error: ".$mcdata->error; 
return ""; // <-- This was obviously from within a function. If you made it here, it was a success 
+0

非常感謝@Oberst!直到你提到它,我沒有遇到任何關於2.0 API的問題。我最初使用的是來自這裏的:'http:// apidocs.mailchimp.com/api/1.3/listsubscribe.func.php'一旦我轉換到那個並且改變了'email_address'到'email'並且使用了'數組「爲它的工作價值:)再次感謝! – MagentoMan 2014-08-29 03:35:13

6

這裏工作簡單MailChimp PHP API 3.0捲曲示例代碼片段

<?PHP 

// put your api key here note the ending text past the - this is your datacenter 
// the datacenter needs to be added into to the url in the curlopt_url (see below) 
$apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-us11"; // my datacenter was "us11" 


// listid goes here - to find this... log into mail chimp go to Lists menu , 
// look to far right of list name for a drop down arrow, select the "Settings" dropdown, 
// scroll to bottom and look for "Unique id for list" 
$list_id = "xxxxxxxxxx"; // web site list 


// the data I used to register (there may be others you can use, check API docs) 
$email = "<<email_address_to_register>>"; 
$fname = "<<first_name>>"; 
$lname = "<<last_name>>"; 


$auth = base64_encode('user:'.$apikey); 


// Notice the value of 'status' is 'pending' 
// I found this via a google search indicating a double opt in subscription process 

$data = array(
'apikey'  => $apikey, 
'email_address' => $email, 
'status'  => 'pending', 
'merge_fields' => array(
'FNAME' => $fname, 
'LNAME' => $lname, 
) 
); 
$json_data = json_encode($data); 

$ch = curl_init(); 

// notice datacenter "us11" comes after the // - make sure you update this to your datacenter (e.g. us2, us7 etc) or you'll get the "wrong datacenter" error. 
$curlopt_url = "https://us11.api.mailchimp.com/3.0/lists/$list_id/members/"; 
curl_setopt($ch, CURLOPT_URL, $curlopt_url); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 
    'Authorization: Basic '.$auth)); 
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); 

$result = curl_exec($ch); 
/* 
// some debug statements 
var_dump($result); 
print_r ($result); 
*/ 


// here is simple way to determine status of a subscription 
// $result is in JSON format 
// this following loop is a simple JSON decode loop I found via google 


$status = "undefined"; 
    $msg = "unknown error occurred"; 
$myArray = json_decode($result, true); 

foreach($myArray as $key => $value) 
{ 

    // debug key<<<=>>>$value<<< <br>"; 

    if($key == "status") 
    { 
     $status=$value; 
     //debug     echo" status found $status<Br>"; 
    } 
    else if ($key == "title") 
    { 
     $msg=$value; 
     //debug     echo" title found $msg<Br>"; 
    } 


} 

// create the output that gets displayed or returned if invoked by AJAX method 
if($status == "pending") 
{ 
    $msg = "Success! <br>$email has been subscribed <Br>check your inbox for the confirmation email to complete your subscription"; 
} 
else 
{ 
    $msg = "Sorry can not subscribe email $email <br>$msg <Br>"; 
} 


echo "$msg <br>"; 


die(' '); // frees up mem etc.. 

?> 
+0

它給了我這個錯誤 [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ [title] => JSON解析錯誤 [status] => 400 [detail] =>我們遇到了未指定的JSON解析錯誤。 – saadk 2016-05-10 07:08:40

+0

因爲MailChimp API的第二版正在停止中,所以現在應該是可接受的答案。 – BFWebAdmin 2016-08-30 10:18:35

+2

有人可以解釋一下爲什麼mailchimp開發者沒有提供這樣一個易用的例子嗎?我想這會很容易,然後... – Black 2016-09-28 14:16:31