Skip to content

Instantly share code, notes, and snippets.

@richogreen
Last active June 20, 2016 04:52
Show Gist options
  • Save richogreen/dcb34a8618859d2785e995642c434de8 to your computer and use it in GitHub Desktop.
Save richogreen/dcb34a8618859d2785e995642c434de8 to your computer and use it in GitHub Desktop.
MailChimp API v3 - Subscribe via cURL/PHP
<?php
/**
* @param $email
* @param $fname
* @param $apikey
* @param $listid
* @param $server - Datacenter associated with account e.g. 'us2'
*/
function mc_subscribe($email, $firstName, $apiKey, $listId, $server)
{
$auth = base64_encode( 'user:'.$apiKey );
$data = array(
'apikey' => $apiKey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $firstName
)
);
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.$listId.'/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
try {
$result = curl_exec($ch);
if($result) {
$json = json_decode($result);
// echo $json->{'status'};
} else {
// ..
}
} catch (Exception $e) {
// ..
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment