Skip to content

Instantly share code, notes, and snippets.

@barbotkin
Last active February 2, 2017 15:15
Show Gist options
  • Save barbotkin/ce504c0c39d1038498882c79808033b2 to your computer and use it in GitHub Desktop.
Save barbotkin/ce504c0c39d1038498882c79808033b2 to your computer and use it in GitHub Desktop.
Mailchimp subscribe php api 3
<?php
$data = [
'email' => $_POST['email'],
'status' => 'subscribed',
'firstname' => $_POST['name'],
];
// accesses key and list
$apiKey = '11111111111111111111111111111111-1234';
$listId = '1234567890';
$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']
]
]);
$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);
curl_close($ch);
// Response.
print_r($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment