【问题标题】:MailChimp API PHP - Add to Interest GroupMailChimp API PHP - 添加到兴趣组
【发布时间】:2012-02-04 09:42:22
【问题描述】:

我目前正在使用适用于 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) 对如何将订阅者添加到兴趣组有些不清楚 - 这里有人有什么想法吗?

【问题讨论】:

    标签: php mailchimp mailchimp-api-v3.0


    【解决方案1】:

    从 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'=>'trevor@example.com'),
          'merge_vars'        => $merge_vars
    ));
    

    【讨论】:

    • 将此标记为正确,以便其他登陆这里的人看到它。感谢您发布 APIv2 版本 :)
    • 听起来您的数组元素中缺少逗号。我在上面的代码中没有看到任何错误。发布你的,我可以看看。
    • 谢谢@TrevorGehman,不知怎的,我错过了分组内部的整个动态,这澄清了它并使我的潜艇工作。
    • 随着 API 升级到 v3,我已将其取消标记为正确,原因与我最初接受它的原因相同 :-) 感谢您从那时到现在帮助过的每个人跨度>
    【解决方案2】:

    对于 MailChimp API v3

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

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

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

    列表 > 相关列表 > 兴趣类别(在子资源下拉列表中)

    那么...

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

    那么...

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

    列表 > 相关列表 > 成员(在子资源下拉列表中)

    那么...

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

    创建成员(按钮)

    该页面将加载会员的详细信息。向下滚动,直到看到“兴趣”数组/对象。在那里你会看到 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
      }
    }
    

    似乎你必须声明每一个“利益”,并说明它是真的还是假的。

    【讨论】:

    • 我将其标记为已接受,因为 API 已移至 v3。如果您需要 v2 的解决方案,请向下滚动到 Trevor Gehman 的答案。感谢您发布此信息,Crimbo
    • 没问题!除了API guide 之外,仍然没有官方文档。所以你只能猜测什么是什么
    【解决方案3】:

    我无法在此页面上获得其他答案。这是我必须使用的合并变量:

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

    【讨论】:

    • 是的,这行得通。我必须弄清楚“id”是什么意思——它是兴趣组 LIST id。我将在下面发布我的代码。
    • 这里也一样。重要的是分组中的数组键。我花了大约一个小时才找到你的答案。遗憾的是,API 没有抛出异常。
    • 请注意,虽然您可以传递分组列表的 'name' 或 'id',但您只能使用 'groups' 的名称(如示例中所示)。不幸的是,您不能在那里传递 ID。 ://
    • 这也是对我有用的答案。对我来说,它为“GROUPINGS”中的数组指定键“0”
    【解决方案4】:

    使用GROUPINGS合并变量:

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

    【讨论】:

    【解决方案5】:

    这是我开始工作的代码

    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";
    }
    

    【讨论】:

      【解决方案6】:

      这是贾斯汀答案的变体,但在尝试了以上所有方法后,这是我唯一可以使用 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);
      

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          使用DrewM's MailChimp wrapper 和数组简写语法的示例(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' => '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' OR 'id' 进行分组,两者都不需要。要获取分组 ID,请使用:lists/interest-groupings

          【讨论】:

            猜你喜欢
            • 2015-12-10
            • 2019-08-29
            • 2013-08-29
            • 2016-04-17
            • 2016-12-11
            • 2018-04-30
            • 2013-09-16
            • 2016-10-03
            • 2016-01-17
            相关资源
            最近更新 更多