繁体   English   中英

MailChimp API PHP - 添加到兴趣组

[英]MailChimp API PHP - Add to Interest Group

我目前正在使用MailChimp API for PHP,版本1.3.1( http://apidocs.mailchimp.com/api/downloads/#php

我在MailChimp中设置了一个列表,并想动态添加:

  1. 列表的订阅者(完成: $objMailChimp->listBatchSubscribe($strMailingListID, ...)
  2. 兴趣分组(完成: $objMailChimp->listInterestGroupingAdd($strMailingListID, ...)
  3. 兴趣组进入这些分组(完成: $objMailChimp->listInterestGroupAdd($strMailingListID, ...)
  4. 分配给相关团体的订户(未完成)

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",
        )
    )
);

对于MailChimp API v3

截至第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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM