MailChimp API PHP - 添加到兴趣组

发布于 2024-12-25 04:53:32 字数 739 浏览 1 评论 0原文

我目前正在使用 PHP 的 MailChimp API,版本 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)对于如何将订阅者添加到兴趣组有些不清楚 - 这里有人有任何想法吗?

I'm currently using the MailChimp API for PHP, version 1.3.1 (http://apidocs.mailchimp.com/api/downloads/#php)

I've set up a list in MailChimp, and would like to dynamically add:

  1. Subscribers to the list (done: $objMailChimp->listBatchSubscribe($strMailingListID, ...))
  2. Interest Groupings (done: $objMailChimp->listInterestGroupingAdd($strMailingListID, ...))
  3. Interest Groups into those Groupings (done: $objMailChimp->listInterestGroupAdd($strMailingListID, ...))
  4. Subscribers assigned to relevant Groups (not done)

The API (http://apidocs.mailchimp.com/api/1.3/#listrelated) is somewhat unclear on how to add a subscriber to an interest group - does anyone here have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

玩套路吗 2025-01-01 04:53:32

从 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/)然后您可以通过列表/将其发送到 MailChimp订阅或列表/批量订阅:

$MailChimp = new MailChimp('API_KEY');
$result = $MailChimp->call('lists/subscribe', array(
      'id'                => 'LIST ID',
      'email'             => array('email'=>'[email protected]'),
      'merge_vars'        => $merge_vars
));

As of version 2.0 of MailChimp's API, this should work:

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

Source: http://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

Using a barebones PHP wrapper (https://github.com/drewm/mailchimp-api/) you can then send this to MailChimp via either the lists/subscribe or lists/batch-subscribe:

$MailChimp = new MailChimp('API_KEY');
$result = $MailChimp->call('lists/subscribe', array(
      'id'                => 'LIST ID',
      'email'             => array('email'=>'[email protected]'),
      'merge_vars'        => $merge_vars
));
じ违心 2025-01-01 04:53:32

对于 MailChimp API v3

从 v3 开始,“分组”已更改为“兴趣”。

您必须找到您想要添加的群组(兴趣)的 ID。不幸的是,这在 MailChimp 仪表板上的任何地方都找不到。

查找“兴趣”ID(而不是创建脚本)的最简单方法是转到 MailChimp Playground,然后输入您的 API 密钥后,路由到...

列表>有问题的列表>兴趣类别(在子资源下拉列表中)

然后...

兴趣类别的兴趣(在子资源下拉列表中)

然后...

点击兴趣并引用“id”字段,忽略其他 ID 字段

OR

列表>有问题的列表>成员(在子资源下拉列表中)

然后...

为任何成员加载(在操作下拉列表中)

创建成员(按钮)

页面将加载会员的详细信息。向下滚动,直到看到“兴趣”数组/对象。在那里您将看到 ID。请注意,它们可以设置为 true 或 false。

您必须通过执行前面的方法或拨打电话,然后通过 MailChimp 仪表板查看成员的详细信息,找出与哪个“群组”/“兴趣”相关的 ID。


因此,当涉及到实际进行 POST 调用(“member”创建)时,您可能需要类似于...

{
  "email_address":"[email protected]",
  "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 调用(“member”编辑) 例子...

{
  "interests": {
    "b8a9d7cbf6": false,
    "5998e44916": true
  }
}

看来你必须声明每一个‘兴趣’,并说明它是真是假。

For MailChimp API v3

As of v3, 'groupings' has changed to 'interests'.

You have to find out the ID of the group (interest) that you are wanting to add to. Unfortunately this cannot be found anywhere on the MailChimp dashboard.

The easiest way to find out the 'interest' ID (rather than creating a script) is to go to the MailChimp playground and then, after entering in your API key, route to...

lists > the list in question > interest-categories (in the sub-resources dropdown)

then...

interests (in the sub-resources dropdown) for interest category

then...

Click through to the interest and refer to the 'id' field, ignoring the other ID fields

OR

lists > the list in question > members (in the sub-resources dropdown)

then...

load (in the actions dropdown) for any member

or

Create Members (button)

The page will load the member's details. Scroll down until you see the 'interests' array/object. There you will see the IDs. Notice they can be set to true or false.

You will have to figure out which ID relates to what 'group'/'interest' by going about the previous method or making the call, and then looking at the member's details via your MailChimp dashboard.


So when it comes to actually making the POST call ('member' create), you would want something on the lines of...

{
  "email_address":"[email protected]",
  "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"
  }
}

A PUT call ('member' edit) example...

{
  "interests": {
    "b8a9d7cbf6": false,
    "5998e44916": true
  }
}

It seems that you must declare every 'interest', and state whether it is true or false.

故事灯 2025-01-01 04:53:32

我无法让此页面上的其他答案发挥作用。这是我必须使用的合并变量:

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

I could not get the other answers on this page to work. Here's the merge vars that I had to use:

$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",
        )
    )
);
好久不见√ 2025-01-01 04:53:32

使用 GROUPINGS 合并变量:

通过分组设置兴趣组。该数组中的每个元素应该是
包含“groups”参数的数组,其中包含逗号
要添加的兴趣组的分隔列表。兴趣组中的逗号
名称应该用反斜杠转义。即“,”=> “\,”或者
用于指定分组的“id”或“name”参数 - 获取自
listInterestGroupings()

Use GROUPINGS merge var:

Set Interest Groups by Grouping. Each element in this array should be
an array containing the "groups" parameter which contains a comma
delimited list of Interest Groups to add. Commas in Interest Group
names should be escaped with a backslash. ie, "," => "\," and either
an "id" or "name" parameter to specify the Grouping - get from
listInterestGroupings()

固执像三岁 2025-01-01 04:53:32

这是我要工作的代码

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 = '[email protected]';
//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";
}

Here's the code I got to work

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 = '[email protected]';
//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";
}
我是有多爱你 2025-01-01 04:53:32

这是 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' => '[email protected]'),
            '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);

This is a variation of Justins answer but having tried all the above this is the only one I could get to work with DrewM's MailChimp wrapper

$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' => '[email protected]'),
            'merge_vars'        => $merge_vars,
            'double_optin'      => true,
            'update_existing'   => true,
            'replace_interests' => false, // do you want to add or replace?
            'send_welcome'      => false,
        )
    );

If you are unsure of your groups lists ID and wish to use it you can call:

$current_groupings = $mc->call( 'lists/interest-groupings', array(
                'id' => '[GROUP_LIST_ID]',
            ) );
var_dump($current_groupings);
荆棘i 2025-01-01 04:53:32

另请注意 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);
}

Also please note optional but very important last parameter of listUpdateMember, by default replace_interests it set to true so it will overwrite any subscription the user you are updating could have had in the past. If you want to add new ones not touching previous ones just pass the new group name you wanna add and set replace_interests to 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);
}
深爱不及久伴 2025-01-01 04:53:32

使用 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' => '[email protected]' ],
            'merge_vars'        => $merge_vars,
            'double_optin'      => true,
            'update_existing'   => true,
            'replace_interests' => false, // do you want to add or replace?
            'send_welcome'      => false,
        ]
    );

您需要对“名称”进行分组OR 'id',两者都不需要。要获取分组 ID,请使用: lists/interest-groupings

Example using DrewM's MailChimp wrapper and shorthand syntax for arrays (PHP 5.4+) add to groups:

$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' => '[email protected]' ],
            'merge_vars'        => $merge_vars,
            'double_optin'      => true,
            'update_existing'   => true,
            'replace_interests' => false, // do you want to add or replace?
            'send_welcome'      => false,
        ]
    );

You need grouping 'name' OR 'id', both are not needed. To get grouping ID use: lists/interest-groupings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文