Skip to content

Instantly share code, notes, and snippets.

@jonesch
Last active December 29, 2015 10:39
Show Gist options
  • Save jonesch/7658610 to your computer and use it in GitHub Desktop.
Save jonesch/7658610 to your computer and use it in GitHub Desktop.
AJAX PHP Processor - Mail Chimp List Subscribe v2.0
//create a clean array of data to be posted
foreach($_POST as $key => $val) {
$user_input[$key] = htmlentities(stripslashes(trim($val)), ENT_QUOTES, 'UTF-8');
}
// There are two unique parts to this URL. The First is the "us3" portion of the URL is specific to your account. The second is the file extension at the end is what is returned, you can set it to json, php, or xml, but json is default.
$post_url = 'https://us3.api.mailchimp.com/2.0/lists/subscribe.json?';
// Let's put together our user data to send
$post_query_array = array(
"apikey" => "APIKEY",
"id" => "LIST-ID",
"email" => array(
"email" => $user_input['usr_email'],
"euid" => "",
"leid" => ""
),
"merge_vars" => array( // Build an Array of the different Merge Vars you setup in your account
'FNAME' => $user_input['usr_first_name'],
'LNAME' => $user_input['usr_last_name']
)
);
// We still need to build our HTML query string to send
$post_query_string = http_build_query($post_query_array);
// Make sure we have a User's Email address as this is the only required item
if(!empty($user_input['usr_email'])){
// Submit the data via Curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url); // Post URL
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query_string); // Our Query string that we built
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_out = curl_exec($ch);
echo $curl_out;
} else {
echo 'failure';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment