2015-05-30 128 views

回答

1

添加/編輯經由MailChimp V3.0 REST API訂戶。

// node/javascript specific, but pretty basic PUT request to MailChimp API endpoint 
 
    
 
// dependencies (npm) 
 
var request = require('request'), 
 
    url = require('url'), 
 
    crypto = require('crypto'); 
 

 
// variables 
 
var datacenter = "yourMailChimpDatacenter", // something like 'us11' (after '-' in api key) 
 
    listId = "yourMailChimpListId", 
 
    email = "subscriberEmailAddress", 
 
    apiKey = "yourMailChimpApiKey"; 
 

 
// mailchimp options 
 
var options = { 
 
    url: url.parse('https://'+datacenter+'.api.mailchimp.com/3.0/lists/'+listId+'/members/'+crypto.createHash('md5').update(email).digest('hex')), 
 
    headers: { 
 
     'Authorization': 'authId '+apiKey // any string works for auth id 
 
    }, 
 
    json: true, 
 
    body: { 
 
     email_address: email, 
 
     status_if_new: 'pending', // pending if new subscriber -> sends 'confirm your subscription' email 
 
     status: 'subscribed',    
 
     merge_fields: { 
 
      FNAME: "subscriberFirstName", 
 
      LNAME: "subscriberLastName" 
 
     }, 
 
     interests: { 
 
      MailChimpListGroupId: true // if you're using groups within your list 
 
     } 
 
    } 
 
}; 
 

 
// perform update 
 
request.put(options, function(err, response, body) { 
 
    if (err) { 
 
     // handle error 
 
    } else { 
 
     console.log('subscriber added to mailchimp list'); 
 
    } 
 
});