2016-12-05 161 views
2

根据文档POST/lists/{list_id}批量订阅或取消订阅列表成员。Mailchimp API 3.0批量订阅

如果我发送两个成员;一个新部件和一个取消订阅构件:

{ 
     "update_existing":true, 
     "members":[ 
      { 
       "email_address":"[email protected]", 
       "email_type":"html", 
       "status":"subscribed" 
      }, 
      { 
       "email_address":"[email protected]", 
       "email_type":"html", 
       "status":"subscribed" 
      } 
     ] 
    } 

的文档(http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#create-post_lists_list_id),指出该所得JSON将包括与new_members,updated_members阵列和阵列成员错误:

Response body parameters

Errors: An array of objects, each representing an email address that could not be added to the list or updated and an error message providing more details.

而是我得到一个HTTP状态400,出现以下错误:

{ 
    "type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error- glossary/", 
    "title":"Member Exists", 
    "status":400, 
    "detail":"[email protected] is in a compliance state due to unsubscribe, bounce, or compliance review and cannot be subscribed.", 
    "instance":"" 
} 
+2

我除了标题相同的反应是:“会员在遵守国家” – nmit026

回答

0

对于Batch subscribe试试这个下面代码。此测试和100%的工作代码。

<?php 
$apikey = ''; // Your Mailchimp ApiKey 
$list_id = ''; // your List ID Where you want to add subscriber 

$servername = 'localhost'; 
$username = 'root'; 
$password = ''; 
$dbname  = 'dada_mail_to_mailchimp'; 
// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die('Connection failed: ' . $conn->connect_error); 
} 

$sql  = 'SELECT * FROM emails Limit 2'; 
$result = $conn->query($sql); 
$finalData = []; 
if ($result->num_rows > 0) { 
    // output data of each row 
    while ($row = $result->fetch_assoc()) { 
     $individulData = array(
      'apikey'  => $apikey, 
      'email_address' => $row['email'], 
      'status'  => $row['status'],//subscribe,pending,unsubscribe 
      'merge_fields' => array(
       'FNAME' => $row['FNAME'], 
       'LNAME' => $row['LNAME'], 
      ) 
     ); 

     $json_individulData  = json_encode($individulData); 
     $finalData['operations'][] = 
      array(
       "method" => "POST", 
       "path" => "/lists/$list_id/members/", 
       "body" => $json_individulData 
      ); 
    } 
} 

$api_response = batchSubscribe($finalData, $apikey); 
print_r($api_response); 
$conn->close(); 

/** 
* Mailchimp API- List Batch Subscribe added function 
* 
* @param array $data Passed you data as an array format. 
* @param string $apikey your mailchimp api key. 
* 
* @return mixed 
*/ 
function batchSubscribe(array $data, $apikey) 
{ 
    $auth   = base64_encode('user:' . $apikey); 
    $json_postData = json_encode($data); 
    $ch   = curl_init(); 
    $dataCenter = substr($apikey, strpos($apikey, '-') + 1); 
    $curlopt_url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/batches/'; 
    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_postData); 

    $result = curl_exec($ch); 
    return $result; 
} 
+0

https://stackoverflow.com/a/42258497/6707985如果你几乎可以直接复制代码请举报了一个问题,重复而不是双重张贴:) – geisterfurz007