mailchimp:如何为特定列表成员设置合并var值

时间:2015-08-07 22:34:10

标签: var mailchimp

我应该使用mailchimp中的哪个API为特定列表成员设置合并变量值?

如果我要添加新的列表成员,我可以使用lists / subscribe为该新成员设置合并var值。但是,如果列表成员已存在,我看不到用于更改值的API。

API list / merge-var-set设置所有列表成员的值。并且lists / merge-var-update设置合并var的属性。

设置特定值的API是什么?

4 个答案:

答案 0 :(得分:1)

在APIv2中,您仍然使用lists/subscribe,但您也会传递update_existing。这是considerably more intuitive in APIv3,如果可以,您应该使用,因为2.0已被正式弃用。

答案 1 :(得分:1)

在Mailchimp API v3中,您可以这样设置合并字段值:

PUT {api_endpoint}/3.0/lists/{list_id}/members/{member_hash}

{
    "merge_fields": {
        "BIRTHDAY": "01/23"
    }
}

其中{member_hash}是成员电子邮件地址的md5哈希值。

您不必提供所有合并字段。 Mailchimp将仅更新您指定的人。

答案 2 :(得分:0)

我花了一些时间盘旋着陆区。也许v3 api Mailchimp, update lists member merge_field key value pair的php类将有助于为某人澄清这一点。

这似乎是不言而喻的,但是如果对我说出不清楚的地方,我会很高兴;我很乐意澄清此帖子。

简而言之,卢卡斯-维克托的答案是正确的;但是,我显然陷入了curl php passthru命令的转义过程。请注意,以下addslashes()方法中的mc::get_url()解决了我的问题。


<?php

// v3 api http basic authentication
class mc {

  public $show_cmd = 1;

  function __construct($api_key) {
    $this->api_key = $api_key;
  }

  function update_member_merge_field($list_id, $mem_id, $fld, $val) {
    //lists/{list_id}/members/{subscriber_hash}
    $uri = 'lists/'.$list_id.'/members/'.$mem_id;
    $data = array(
      'merge_fields' => array(
        $fld => $val,
      )
    );
    $json = $this->get_url($uri, 'PATCH', $data);
    die('<pre>'.print_r($json->merge_fields,true).PHP_EOL);
  }

  function get_list_members($list_id, $show_everything=0) {
    //lists/{list_id}
    $uri = 'lists/'.$list_id.'/members';
    $json = $this->get_url($uri);
    if (empty($json->members)) {
      exit('hmmm, do you have any members in this list?'.PHP_EOL);
    } else {
      foreach ($json->members as &$mem) {
        echo ' use id "'.$mem->id.'" for member "'.substr($mem->email_address, 0, 100).PHP_EOL;
      }
    }
    if (! empty($show_everything)) {
      die('<pre>'.print_r($json,true).PHP_EOL);
    }
  }

  function get_lists($show_everything=0) {
    //lists
    $uri = 'lists/';
    $json = $this->get_url($uri);
    if (empty($json->lists)) {
      exit('hmmm, do you have any lists?'.PHP_EOL);
    } else {
      foreach ($json->lists as &$list) {
        echo ' use id "'.$list->id.'" for list "'.substr($list->name, 0, 100).PHP_EOL;
      }
    }
    if (! empty($show_everything)) {
      die('<pre>'.print_r($json,true).PHP_EOL);
    }
  }

  private function get_url($uri, $method='GET', $data='') {
    $x = $this->api_key;
    $pos = strrpos($x, '-');
    $datacenter = substr($x, ($pos + 1));
    if ($method == 'GET') {
      // be aware of pagination...count is 10 by default...not sure of max, but 100 works
      $uri .= '?offset=0&count=100';
    }

    // be careful about accepting unclean input here
    // this is designed for developer use only where
    // the four vars: method, datacenter, uri, and x
    // are know to contain only appropriate content!

    $cmd = "curl --request $method --url 'https://$datacenter.api.mailchimp.com/3.0/$uri' --user 'apijuju:$x'";
    if (! empty($data)) {
      $cmd .= ' --data '.addslashes(json_encode($data));
    }

    if ($this->show_cmd) echo $cmd.'<br><br>';

    ob_start(); passthru($cmd); $out = ob_get_contents(); ob_end_clean();
    $ret = json_decode(trim($out));

    return $ret;
  }
}

/*
// EXAMPLE USAGE:

// to find api_key use dropdown under profile pic...
// find it under "Account" => "Extras" => "API Keys"
$api_key = 'examplenhg93ehqle33elqhik32nh-us1';

// to find $list_id see mailchimp::get_lists(1);
$list_id = 'exampleihqle39';
// to find $mem_id see mailchimp::get_list_members($list_id, 1);
$mem_id = 'examplehk32nhg93elqhiexamplehk32nhg93elqhi';

$merge_field = 'NAME';
$merge_value = 'Joe Momma';

$mc = new mailchimp($api_key);
$mc->update_member_merge_field($list_id, $mem_id, $merge_field, $merge_value);
*/

答案 3 :(得分:0)

使用Mailchimp api v3的npm软件包

诅咒Mailchimp 30分钟后,我意识到您需要在merge_fields内发送body对象。否则它将无声地失败-_-'

不起作用

mailchimp.patch({
    path: '/lists/666trolol0/members/' + emailHash,
    "merge_fields" : { 
        "FNAME:": "whatever" 
    }
}, ...

可行

mailchimp.patch({
    path: '/lists/666trolol0/members/' + emailHash,
    body: {
        "merge_fields" : { 
            "FNAME:": "whatever" 
        }
    }
}
}, ...