如何在Mailchimp上向列表成员添加标记?

问题描述 投票:0回答:2

我想在mailchimp列表中为特定用户添加标记

$email = "[email protected]";
$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 Object
(
    [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
    [title] => Invalid Resource
    [status] => 400
    [detail] => Expected argument of type "array or Traversable and ArrayAccess", "string" given
    [instance] => XXXXX-XXXXX-XXXXX
)
php mailchimp mailchimp-api-v3.0
2个回答
1
投票
// 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
投票

我不得不使用嵌套数组来使标签工作:

$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' 
    )
  )
);
© www.soinside.com 2019 - 2024. All rights reserved.