Skip to content

Instantly share code, notes, and snippets.

@jonesch
Created October 18, 2012 11:45
Show Gist options
  • Save jonesch/3911293 to your computer and use it in GitHub Desktop.
Save jonesch/3911293 to your computer and use it in GitHub Desktop.
Get List ID & Submit Email/Subscriber to MailChimp via cURL
<?php
$api_key = 'API KEY';
$prefix = 'usX'; //at the end of your API Key, there is a -us1, or us2, etc......you want the prefix to be the us2 for examples.
//Let's go Get a LIST ID for the subscriber list we are going to be putting content in.
$get_lists = 'http://'.$prefix.'.api.mailchimp.com/1.3/?method=lists';
$data = array();
$data['apikey'] = $api_key;
$post_str = '';
foreach($data as $key=>$val) {
$post_str .= $key.'='.urlencode($val).'&';
}
$post_str = substr($post_str, 0, -1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $get_lists);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
$output = json_decode($response);
$list_id = $output->data[0]->id; //this dives into our first list ID.
//Now let'see if we have a List ID to work with, and then submit the subscriber
if(!empty($list_id)){
$push_subscriber = 'http://'.$prefix.'.api.mailchimp.com/1.3/?method=listSubscribe';
$data = array();
$data['apikey'] = $api_key;
$data['id'] = $list_id;
$data['email_address'] = '[email protected]';
$data['merge_vars[FNAME]'] = 'Christopher';
$data['merge_vars[LNAME]'] = 'Jones';
$data['output'] = 'json';
$post_str = '';
foreach($data as $key=>$val) {
$post_str .= $key.'='.urlencode($val).'&';
}
$post_str = substr($post_str, 0, -1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $push_subscriber);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
$output = json_decode($response); //will return a 1 or 0, true or false.
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment