将标签添加到通过api php创建的mailchimp订户

时间:2018-09-25 10:37:31

标签: php mailchimp-api-v3.0

我正在使用PHP Curl代码将新订户添加到MailChimp。它正在向订户添加所有信息,但我没有获得如何通过相同的Curl调用添加标签的方法。我已经尝试了很多方法,但是没有任何效果。 请让我知道如何将标签添加到该新订户。

3 个答案:

答案 0 :(得分:1)

这是将用户注册到黑猩猩列表并添加标签的有效示例:

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

答案 1 :(得分:0)

根据MailChimp文档,您需要使用单独的调用来添加标签。 https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/ 请注意,在我的测试中,即使添加了标签,响应消息也为空。我做错了什么,或者API中有错误。

答案 2 :(得分:0)

使用PHP发送POST / lists / {list_id} / members / {subscriber_hash} / tags的我的解决方案 -希望这对某人有帮助,这让我发疯了

这使用了Drewm的MailChimp包装器类v3

https://github.com/drewm/mailchimp-api   -我不知道如何设置,所以我安装了此插件

https://wordpress.org/plugins/nmedia-mailchimp-widget/   -我从未使用过,但是它成功设置了您需要做的一切   您的网站由Drewm指定! 2分钟

为了安全起见,我确实在第23行的/classes/mailchimp/Mailchimp.php中将2.0修改为3.0

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

注意:list_id是PLAYGROUND LIST ID,而不是您在网络界面中看到的ID

因此,您基本上不需要知道TAG的ID#(简简“ thename”)并将其设置为活动或不活动

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