2016-07-27 110 views
5

我正尝试更新我的代码,以便将新用户订阅到我的MailChimp新闻邮件。无法将用户添加到使用MailChimp API 3.0的组中

我希望将用户添加到组中,以便在发送新闻通讯时能够区分用户。但是,此代码会订阅用户,但不会将其添加到任何组。我究竟做错了什么?我正在使用MailChimp API 3.0。

我的代码:

$apiKey  = 'XXXX'; 
$list_id = 'XXXX'; 
$memberId = md5(strtolower($data['email'])); 
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1); 

$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . $memberId; 

$json = json_encode(array(
    'email_address' => $data['email'], 
    'status'  => $data['status'], // "subscribed","unsubscribed","cleaned","pending" 
    'merge_fields' => array(
     'FNAME'   => $data['firstname'], 
     'LNAME'   => $data['lastname'], 
     'GROUPINGS'  => array(
      0 => array(
       'name' => 'Interests', 
       'groups' => array($data['tag']), 
      ), 
     ), 
    ), 
)); 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); 

$result = curl_exec($ch); 
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch); 

回答

6

你逝去的利益集团的错误的阵列英寸 您需要将值为trueinterests数组中的利息组ID标识为true

$json = json_encode(array(
    'email_address' => '[email protected]', 
    'status'  => 'subscribed', 
    'merge_fields' => array(
     'FNAME'   => 'FirstName', 
     'LNAME'   => 'LastName', 
    ), 
    'interests'  => array( 
     '9143cf3bd1' => true, 
     '789cf3bds1' => true 
    ), 
)); 

您将通过请求该URL

/lists/{list_id}/interest-categories 

see doc here

获取利益集团[在上面的例子中9143cf3bd1,789cf3bds1]列表中的id如果你想删除的用户组,您需要在更新请求中将其值设置为false

+0

穆罕默德您好, 我的代码是: 阵列( \t 'EMAIL_ADDRESS'=> '[email protected]', \t '状态'=> '挂起', \t 'merge_fields'=>数组( \t \t 'FNAME'=> '姓', \t \t 'L-NAME'=> 'LASTNAME', ) '利益'=>数组( '8c4bb7fbfd'=>真 ) ) 但它仍不会将用户添加到兴趣组。它给了我200头。 –

+1

对不起,它工作完美!谢谢。 –

+0

很高兴知道@TobiasChristianJensen :) –

相关问题