add tags to mailchimp subscriber created via api php

我怕爱的太早我们不能终老 提交于 2020-01-03 06:59:07

问题


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.


回答1:


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



回答2:


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.




回答3:


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 = '{ "tags": [ { "name": "youtube", "status": "active" },  { "name": "vimeo", "status": "active" } ] }';

// $payload = '{ "tags": [ { "name": "$tag1_name", "status": "active" },  { "name": "$tag1_name", "status": "active" } ] }';
//    add as many tags as you like - make forwhile ifeach whatever you want 

$payload_json = json_decode($payload, true);
$result = $MailChimp->post("lists/$list_id/members/$hashed/tags", $payload_json);


//   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   


来源:https://stackoverflow.com/questions/52496160/add-tags-to-mailchimp-subscriber-created-via-api-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!