add tags to mailchimp subscriber created via api php-ThrowExceptions

Exception or error:

I am using the PHP Curl code to add new subscriber to MailChimp. It is adding the subscriber with all information but I am not getting how to add the tags via with same Curl call. I have tried in many ways but nothing worked for me.
Please let me know how can I add the tags to this new subscriber.

How to solve:

According to the MailChimp documentation, you need to use a separate call to add tags. https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/
Please note that in my testing, the response message is empty, even though the tag is added. Either I’m doing something wrong or there’s a bug in the API.

Answer:

This is working example of registering a user to mail-chimp list and adding tags:

        $api_key = 'YOUR_API_KEY';
        $list_id = 'YOUR_LIST_ID';
        $email =  'USER_EMAIL';
        /**
        *  Possible Values for Status:
        *  subscribed, unsubscribed, cleaned, pending, transactional
        **/
        $status = 'subscribed'; 
        if($email) {
            $data = array(
              'apikey'        => $api_key,
              'email_address' => $email,
              'status'     => $status,
              'tags'  => array('your tag name'),
              'merge_fields'  => array(
                    'FNAME' => $fname,
                    'LNAME' => $lname
                  )
            );

          // URL to request
          $API_URL =   'https://' . substr($api_key,strpos($api_key,'-') + 1 ) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address']));

          $ch = curl_init(); 
          curl_setopt($ch, CURLOPT_URL, $API_URL);
          curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
          curl_setopt($ch, CURLOPT_TIMEOUT, 10);
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data) ); 
          $result = curl_exec($ch);  
          curl_close($ch);

          $response = json_decode($result);
        }

Answer:

My Solution to sending POST /lists/{list_id}/members/{subscriber_hash}/tags with PHP
– I hope this helps someone, this drove me insane

This uses Drewm’s MailChimp wrapper class v3

https://github.com/drewm/mailchimp-api
– which I had no idea how to set up, so instead I installed this Plug-in

https://wordpress.org/plugins/nmedia-mailchimp-widget/
– which I never used, but it successfully sets up everything you needed to do into
your website as specified by Drewm! 2mins

I did edit the 2.0 to 3.0 in /classes/mailchimp/Mailchimp.php on line 23 just to be safe

public $root = ‘https://api.mailchimp.com/3.0‘;

NOTE : list_id refers to the PLAYGROUND LIST ID – not the ID you see in the web interface

SO essentially you don’t need to know the ID# for the TAG, plain jane “thename” and set it to active or inactive

function add_tags()  {

use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('271afffe3a_myfancyid-us8');
$list_id='dsgf350h53';  //  <--- PLAYGROUND LIST_ID
$hashed= md5('pickle@mypicklewish.org');

$payload = array( 'tags' =>
    array(
        array('name' => 'upgraded', 'status' => 'active' ),
    ),
);

$result = $mailchimp->post( "lists/$list_id/members/$hashed/tags", $payload );


//   this error success produces NOTHING in the Response from MailChimp
// because it doesn't seem to send anything, but if not error then success I guess 
// so you can still capture its status 
if ($MailChimp->success()) {
    // do whatever
    echo 'Success<br>';
    print_r($result);   
} else {
    // do whatever
    echo 'Error<br>';
    print_r($result);
    echo $MailChimp->getLastError();
}

}  // end function

// set status as either active or inactive for add or remove
// so you can load this however you like to manage users throughout your site   

Leave a Reply

Your email address will not be published. Required fields are marked *