How to add a tag to a list member on Mailchimp?
Asked Answered
F

2

6

I want to add a tag to a specific user on a mailchimp list

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

Do you know what is the issue ? I have this return :

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
)
Froggy answered 18/2, 2019 at 17:20 Comment(0)
M
7

I had to use a nested array for the tags to work:

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

Without the nested array( array('name' => $tagname,'status' => 'active' ) ) MailChimp returns the following error message:

'Invalid Resource', 400, 'Expected argument of type "array or Traversable and ArrayAccess", "string" given'

Multiple tags can be set this way:

$tagname1 = "TAG_NAME_1";    
$tagname2 = "TAG_NAME_2"

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

Say:

$tagname1 = "TAG_NAME_1";
$tagname2 = "TAG_NAME_2";

This will give the following JSON representation of the $data variable:

{"tags":[{"name":"TAG_NAME_1","status":"active"},{"name":"TAG_NAME_2","status":"active"}]}
Municipalize answered 21/5, 2019 at 20:36 Comment(4)
Could you add the JSON representation which would make the answer useful for other implementation languages, too?Roband
@Roband JSON representation has been added to the answer above.Municipalize
@Municipalize Thanks for adding this JSON representation it really helped me sort the issue in PythonPhthalocyanine
this "works" in that mailchimp replies "success" but the tag is not added. Any ideas?Ose
O
5
// 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'
        )
    )
);
Outgeneral answered 12/4, 2019 at 3:57 Comment(1)
this "works" in that mailchimp replies "success" but the tag is not added. Any ideas?Ose

© 2022 - 2024 — McMap. All rights reserved.