簡體   English   中英

向通過 api php 創建的 mailchimp 訂閱者添加標簽

[英]add tags to mailchimp subscriber created via api php

我正在使用 PHP Curl 代碼向 MailChimp 添加新訂閱者。 它正在向訂閱者添加所有信息,但我不知道如何通過相同的 Curl 調用添加標簽。 我嘗試了很多方法,但沒有任何效果對我有用。 請讓我知道如何將標簽添加到這個新訂閱者。

我使用 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

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

注意:list_id 指的是 PLAYGROUND LIST ID - 不是您在 Web 界面中看到的 ID

所以基本上你不需要知道標簽的 ID#,簡單的 jane "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 = 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   

這是將用戶注冊到郵件黑猩猩列表並添加標簽的工作示例:

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

根據 MailChimp 文檔,您需要使用單獨的調用來添加標簽。 https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/請注意,在我的測試中,即使添加了標簽,響應消息也是空的。 要么我做錯了什么,要么 API 中存在錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM