Skip to content

Instantly share code, notes, and snippets.

@juanjdlt
Created July 13, 2015 23:48
Show Gist options
  • Save juanjdlt/b781379d5101f121ca5f to your computer and use it in GitHub Desktop.
Save juanjdlt/b781379d5101f121ca5f to your computer and use it in GitHub Desktop.
Mailchimp API v3: Save member to a specific list in PHP
<?php
/* RESOURCES
* API Overview: http://kb.mailchimp.com/api/article/api-3-overview
* Endpoint doc: http://kb.mailchimp.com/api/resources/lists/members/lists-members-collection
* I highly recommend to check out the API Playground and check the available params and how to create the call depending on you need:
* http://kb.mailchimp.com/api/article/about-the-playground
*/
$mc_api_key = MAILCHIMP_API_KEY;
$mailchimp_url_endpoint = 'https://us3.api.mailchimp.com/3.0/lists/547a212260/members';
$current_date_time = new DateTime("now", new DateTimeZone('America/Mexico_City'));
$data = array(
'email_address' => $email,
'status' => 'subscribed',
'ip_opt' => $_SERVER['REMOTE_ADDR'],
'timestamp_opt' => $current_date_time->format(DateTime::ISO8601),
'merge_fields' => array('FNAME' => $firstName, 'LNAME' => $lastName),
'language' => 'es'
);
$data_as_json = json_encode($data);
$ch = curl_init($mailchimp_url_endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: api-key ' . $mc_api_key));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_as_json);
$response = curl_exec($ch);
$call_info = curl_getinfo($ch);
curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment