[英]MailChimp API PHP - Add to Interest Group
我目前正在使用MailChimp API for PHP,版本1.3.1( http://apidocs.mailchimp.com/api/downloads/#php )
我在MailChimp中設置了一個列表,並想動態添加:
$objMailChimp->listBatchSubscribe($strMailingListID, ...)
) $objMailChimp->listInterestGroupingAdd($strMailingListID, ...)
) $objMailChimp->listInterestGroupAdd($strMailingListID, ...)
) API( http://apidocs.mailchimp.com/api/1.3/#listrelated )在如何將訂戶添加到興趣小組方面有些不清楚 - 這里的任何人都有任何想法嗎?
從MailChimp的API 2.0版開始,這應該有效:
$merge_vars = array(
'GROUPINGS' => array(
array(
'name' => "GROUP CATEGORY #1", // You can use either 'name' or 'id' to identify the group
'groups' => array("GROUP NAME","GROUP NAME")
),
array(
'name' => "GROUP CATEGORY #2",
'groups' => array("GROUP NAME")
)
)
);
資料來源: http : //apidocs.mailchimp.com/api/2.0/lists/subscribe.php
使用准系統PHP包裝器( https://github.com/drewm/mailchimp-api/ ),然后您可以通過lists / subscribe或lists / batch-subscribe將其發送到MailChimp:
$MailChimp = new MailChimp('API_KEY');
$result = $MailChimp->call('lists/subscribe', array(
'id' => 'LIST ID',
'email' => array('email'=>'trevor@example.com'),
'merge_vars' => $merge_vars
));
我無法在此頁面上獲得其他答案。 這是我必須使用的合並變量:
$merge_vars = array(
'GROUPINGS' => array(
0 => array(
'id' => "101", //You have to find the number via the API
'groups' => "Interest Name 1, Interest Name 2",
)
)
);
截至第3季度,“分組”已經變為“利益”。
您必須找出要添加到的組(興趣)的ID。 不幸的是,在MailChimp儀表板上的任何地方都找不到。
查找“興趣”ID(而不是創建腳本)的最簡單方法是轉到MailChimp游樂場 ,然后在輸入API密鑰后,路由到...
列表>有問題的列表>興趣類別(在子資源下拉列表中)
然后...
利息類別的利益(在子資源下拉列表中)
然后...
點擊興趣並參考“id”字段,忽略其他ID字段
要么
列表>有問題的列表>成員(在子資源下拉列表中)
然后...
加載(在操作下拉列表中)任何成員
要么
創建會員(按鈕)
該頁面將加載該成員的詳細信息。 向下滾動,直到看到'interests'數組/對象。 在那里你會看到ID。 請注意,它們可以設置為true或false。
您必須通過執行上一個方法或撥打電話,然后通過MailChimp信息中心查看會員的詳細信息,找出與“組”/“興趣”相關的ID。
因此,當涉及到實際進行POST調用('成員'創建)時,你會想要一些......
{
"email_address":"example@freddiesjokes.com",
"status":"subscribed",
"interests": {
"b8a9d7cbf6": true,
"5998e44916": false
},
# ADDITIONAL FIELDS, IF REQUIRED...
"merge_fields":{
"FNAME": "foo bar",
"LNAME": "foo bar",
"MERGE3": "foo bar",
"MERGE4": "foo bar"
}
}
PUT調用('成員'編輯)示例...
{
"interests": {
"b8a9d7cbf6": false,
"5998e44916": true
}
}
您似乎必須聲明每個“利益”,並說明它是真還是假。
這是我開始工作的代碼
require_once 'MCAPI.class.php';
require_once 'config.inc.php'; //contains apikey
// use this once to find out id of interest group List
//$retval = $api->listInterestGroupings($listId);
//echo '<pre>';
//print_r($retval);
//echo '</pre>';
//die();
$emailAddress = 'info@example.com';
//You have to find the number via the API (id of interest group list)
$interestGroupListId = FILLMEIN;
$api = new MCAPI($apikey);
// Create an array of Interest Groups you want to add the subscriber to.
$mergeVars = array(
'GROUPINGS' => array(
0 => array(
'id' => $interestGroupListId,
'groups' => "FILL IN GROUP NAMES",
)
)
);
// Then use listUpdateMember to add them
$retval = $api->listUpdateMember($listId, $emailAddress, $mergeVars);
if ($api->errorCode){
echo "Unable to update member info!\n";
echo "\tCode=".$api->errorCode."\n";
echo "\tMsg=".$api->errorMessage."\n";
} else {
echo "Returned: ".$retval."\n";
}
這是Justins回答的一個變種,但嘗試了以上所有這是我唯一可以使用DrewM的MailChimp包裝器
$merge_vars = array(
'GROUPINGS' => array(
0 => array(
'id' => '[GROUP_LIST_ID]', // need grouping ID
'name' => '[OR_GROUP_LIST_NAME]', // or name instead
'groups' => array( '[GROUP_NAME_1]', '[GROUP_NAME_2]' )
)
),
);
$mc = new MailChimp('[YOUR_MAILCHIMP_API]');
$mc->call(
'lists/subscribe', array(
'id' => '[YOUR_LIST_ID]', // list ID
'email' => array( 'email' => 'someone@something.com'),
'merge_vars' => $merge_vars,
'double_optin' => true,
'update_existing' => true,
'replace_interests' => false, // do you want to add or replace?
'send_welcome' => false,
)
);
如果您不確定您的群組列表ID並希望使用它,您可以致電:
$current_groupings = $mc->call( 'lists/interest-groupings', array(
'id' => '[GROUP_LIST_ID]',
) );
var_dump($current_groupings);
使用GROUPINGS
合並var:
通過分組設置興趣組。 此數組中的每個元素都應該是一個包含“groups”參數的數組,該參數包含要添加的逗號分隔的興趣組列表。 興趣組名稱中的逗號應使用反斜杠進行轉義。 即,“,”=>“\\”,以及“id”或“name”參數來指定分組 - 從listInterestGroupings()獲取
另請注意listUpdateMember的可選但非常重要的最后一個參數,默認情況下, replace_interests設置為true,因此它將覆蓋您正在更新的用戶過去可能擁有的任何訂閱。 如果你想添加不接觸前一個的新組,只需傳遞你要添加的新組名並將replace_interests設置為false。
$api = new MailChimp('API_KEY');
$pArray = array('GROUPINGS'=>array(
array('name'=>'GROUP CATEGORY', 'groups'=>'group1,group2'),
)
);
if (is_array($pArray)) {
$api->listUpdateMember($pListId, $pEmail, $pArray, '', false);
}
使用DrewM的MailChimp包裝器和數組的簡寫語法(PHP 5.4+)的示例添加到組 :
$merge_vars = [
'GROUPINGS' => array(
0 => [
'id' => 12345, // need grouping ID
'name' => 'Interests', // or name instead
'groups' => [ 'cars', 'trucks' ]
]
]
];
$mc = new MailChimp('API_KEY');
$mc->call(
'lists/subscribe', [
'id' => '9876', // list ID
'email' => array[ 'email' => 'example@abc.com' ],
'merge_vars' => $merge_vars,
'double_optin' => true,
'update_existing' => true,
'replace_interests' => false, // do you want to add or replace?
'send_welcome' => false,
]
);
您需要分組'name' 或 'id',兩者都不需要。 要獲取分組ID使用: 列表/興趣分組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.