Adding subscribers to a list using Mailchimp's API v3
Based on the List Members Instance docs, the easiest way is to use a PUT
request which according to the docs either "adds a new list member or updates the member if the email already exists on the list".
Furthermore apikey
is definitely not part of the json schema and there's no point in including it in your json request.
Also, as noted in @TooMuchPete's comment, you can use CURLOPT_USERPWD
for basic http auth as illustrated in below.
I'm using the following function to add and update list members. You may need to include a slightly different set of merge_fields
depending on your list parameters.
$data = [
'email' => 'johndoe@example.com',
'status' => 'subscribed',
'firstname' => 'john',
'lastname' => 'doe'
];
syncMailchimp($data);
function syncMailchimp($data) {
$apiKey = 'your api key';
$listId = 'your list id';
$memberId = md5(strtolower($data['email']));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
$json = json_encode([
'email_address' => $data['email'],
'status' => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => [
'FNAME' => $data['firstname'],
'LNAME' => $data['lastname']
]
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpCode;
}
Python: Adding subscribers to mailchimp with bulk API V3
This is the correct way
from mailchimp3 import MailChimp
def export_to_mailchimp():
#Get all users in DB
users = User.objects.all()
#Create list for dictionaries
operations = []
#loop through queryset
for userobject in users:
#Create dictionary for body
databody_item = {
"email_address": item.email,
"status": "subscribed",
"merge_fields": {
"FNAME": item.first_name,
"LNAME": item.last_name
}
}
#Create dictionary for a operation
operation_item = {"method":"POST", "path":"/lists/000000/members/", "body":json.dumps(databody_item)}
#Append to list
operations.append(operation_item)
client = MailChimp(settings.MAILCHIMP_USER, settings.MAILCHIMP_KEY)
batch = client.batches.create(data={"operations": operations})
Related Topics
Variable Position in Comparision in PHP
Loop Code for Each File in a Directory
How to Send 500 Internal Server Error Error from a PHP Script
How to Convert Seconds to Time Format
Get Current Url Path with Query String in PHP
PHP Namespace with Dynamic Class Name
How to Properly Use While Loop in Pdo Fetchall
How to Set a Default Attribute Value for a Laravel/Eloquent Model
Get Text from <Option> Tag Using PHP
How to Decode Eval( Gzinflate( Base64_Decode(
Cakephp-3.X: How to Change the Data Type of a Selected Alias
Why Do I Have to Run "Composer Dump-Autoload" Command to Make Migrations Work in Laravel
How to Solve Time Out in PHPmyadmin
Why Does PHP Convert a String with the Letter E into a Number
Multiple Index Variables in PHP Foreach Loop