Last active
April 30, 2016 15:25
-
-
Save element121/82bca66fb4233629f003d4c271b92d7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// PHP Code to add a member to a MailChimp list using their API v3.0 | |
// As described in this blog post: http://element121.com/2016/04/30/how-to-add-a-subscriber-using-mailchimps-api-with-php/ | |
// http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/ | |
/* | |
curl --request POST \ | |
--url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members' \ | |
--user 'anystring:apikey' \ | |
--header 'content-type: application/json' \ | |
--data '{"email_address":"urist.mcvankab+3@freddiesjokes.com", "status":"subscribed"}' \ | |
--include | |
*/ | |
// Your ID and token, you will need to REPLACE this with your own | |
$authToken = 'Basic cGFtaVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV3ZGUwNS11czI='; | |
// The new member data to send to the API, REPLACE this with your email address | |
$postData = array( | |
'email_address' => 'test@test.com', | |
'status' => 'subscribed' | |
); | |
// List id from MailChimp obtained from doing a GET on http://us2.api.mailchimp.com/3.0/lists | |
// REPLACE the 999999999 with yours | |
$list_id = "999999999"; | |
// Setup cURL // Note the URL should be https, but that might not work locally. | |
// us2 is obatined from the last part of your API key | |
$ch = curl_init('https://us2.api.mailchimp.com/3.0/lists/'. $list_id .'/members'); | |
curl_setopt_array($ch, array( | |
CURLOPT_POST => TRUE, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_HTTPHEADER => array( | |
'Authorization: '.$authToken, | |
'Content-Type: application/json' | |
), | |
CURLOPT_POSTFIELDS => json_encode($postData) | |
)); | |
// Send the request | |
$response = curl_exec($ch); | |
// Check for errors | |
if($response === FALSE){ | |
die(curl_error($ch)); | |
} | |
// Decode the response | |
$responseData = json_decode($response, TRUE); | |
// Print the date from the response | |
var_dump($responseData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment