Skip to content

Instantly share code, notes, and snippets.

@itsme-renee
Created April 22, 2014 13:02
Show Gist options
  • Save itsme-renee/11178300 to your computer and use it in GitHub Desktop.
Save itsme-renee/11178300 to your computer and use it in GitHub Desktop.
Mail Chimp Api subscribe to list example
<?php
/**
* First things first
*
* get the mailchimp api php package (you can use composer or just download it)
*
* https://packagist.org/packages/mailchimp/mailchimp
*
* Put it somewhere that you can access it and then instantiate it. (see the require_once line)
*
*/
if(!isset($_POST['email'])){
echo json_encode(array('error' => 'must be a post and expecting [email] param'));
exit;
}
require_once "mailchimp-api-php/src/Mailchimp.php";
try {
$mail_chimp = new Mailchimp(''); //api key
$list_id = ''; //list id
$email = trim($_POST['email']);
$result = $mail_chimp->call('lists/subscribe', array('id' => $list_id, 'email' => array('email' => $email),));
if(isset($result['email'])){
echo json_encode(array('success' => 'all is well'));
}else{
echo json_encode(array('error' => 'aw man'));
}
} catch (Exception $e) {
echo json_encode(array('error' => 'catchable error'));
}
exit;
@grantstandridge
Copy link

Much neat. So succinct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment