如何在Mailchimp上的列表成员中添加标签?

时间:2019-02-18 17:21:00

标签: php api tags mailchimp

我想为mailchimp列表上的特定用户添加标签

$email = "toto@example.com";
$tag="test";
$userid = md5( strtolower( $email ) );


$data = array(
    'apikey'        => $mailchimp_api_key,
    'email_address' => $email,
    'tags' => array(
        'name' => $tag,
        'status' => 'active'
        )
    );

$json_data = json_encode($data);

$url = 'https://'.$mailchimp_datacenter.'api.mailchimp.com/3.0/lists/'.$mailchimp_list_id.'/members/' . $userid . '/tags';


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
    'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
if ($displaytaglist!="") {
    curl_setopt($ch, CURLOPT_POST, true);   
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);

您知道问题是什么吗? 我有这个回报:

stdClass对象 (     [类型] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/     [title] =>无效的资源     [状态] => 400     [detail] =>给出类型为“ array or Traversable and ArrayAccess”,“ string”的期望参数     [实例] => XXXXX-XXXXX-XXXXX )

2 个答案:

答案 0 :(得分:0)

// Tags should be passed as multidimensional array. Replace your data variable with this and it will work. No need to pass email & apikey in data variable. Tested.

$data = array(
    'tags' => array(
        array(
            'name' => $tag,
            'status' => 'active'
        )
    )
);

答案 1 :(得分:0)

我必须使用嵌套数组才能使标签起作用:

$data = array(
  'tags' => array(
    array(
      'name' => $tagname,
      'status' => 'active' 
    )
  )
);

没有嵌套数组(array('name'=> $ tagname,'status'=>'active')) MailChimp返回以下错误消息:

“无效资源”,400,“类型为“数组或Traversable和ArrayAccess”的预期参数,给出了“字符串””

可以通过以下方式设置多个标签:

$data = array(
  'tags' => array(
    array(
      'name' => $tagname1,
      'status' => 'active' 
    ),
    array(
      'name' => $tagname2,
      'status' => 'active' 
    )
  )
);