Skip to content

Instantly share code, notes, and snippets.

Created July 17, 2016 20:08
Show Gist options
  • Save anonymous/085a7c83aa133f6e9dac9bd4971a44ef to your computer and use it in GitHub Desktop.
Save anonymous/085a7c83aa133f6e9dac9bd4971a44ef to your computer and use it in GitHub Desktop.
Mailchimp Subcribe to List PHP
<?php
// Populate using POST vars
$data = [
'email' => '[email protected]',
'status' => 'subscribed'
];
var_dump( syncMailchimp($data) );
function syncMailchimp($data) {
$apiKey = 'test-us13';
$listId = 'test';
$memberId = md5(strtolower($data['email']));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/';
$json = json_encode([
'email_address' => $data['email'],
'status' => $data['status']
]);
$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, 'POST');
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 $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment