Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add tags to mailchimp subscriber created via api php

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.

like image 324
Salman Mohammad Avatar asked Oct 15 '25 03:10

Salman Mohammad


1 Answers

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);
        }
like image 54
Seefan Avatar answered Oct 17 '25 17:10

Seefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!