Tag Archives: API
Mailchimp Custom Subscribe Example
Setting up the PHP code to use Mailchimp’s API
By Using the below PHP script and calling it using a URL variable to carry the email address value across, you have a basic mail list member subscription function for mailchimp using their API v3.0
You could for example call this script using some jquery to process a nice neat ajax request to the user can subscribe without experiencing page reloads.
function _sendRequest($method, $parameters) { // Construct array of parameter key and value pairs $pairs = json_encode($parameters); // Setup CURL and execute the HTTP request to mailchimp's API $handle = curl_init(); curl_setopt($handle, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/' . $method); curl_setopt($handle, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Authorization: [ADD YOUR USERNAME HERE] [ADD YOUR API KEY HERE]', 'Content-Length: ' . strlen($pairs)) ); curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, 'POST' ); //curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, $type ); curl_setopt( $handle, CURLOPT_TIMEOUT, 5 ); curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true ); curl_setopt($handle, CURLOPT_POSTFIELDS, $pairs); $result = curl_exec($handle); curl_close($handle); //returns the result as a JSON encoded string return $result; } //setup the member parameters to pass into the function using the email address passed to the script via the URL variable $_GET['email'] $members = array('email_address'=>$_GET['email'], 'status'=>'subscribed'); //execute the function using the API method to subscribe to a particular list, and the parameters from the above line ($members) and echo the result. REMEMBER to add your list ID in the below line. echo _sendRequest('lists/[ADD YOUR LIST ID HERE]/members', $members) ;
Calling the function
Now that the PHP is done, save that as a file on its own, or place the function where ever you like and create a file that calls it using the lines to create the parameters as $members, and echo back the funtion call _sendRequest(‘lists/[ADD YOUR LIST ID HERE]/members’, $members).
Putting it to work
Once thats done, you can create your html input field and button on the web page your want and use some jquery like the example below to put it all together (obviously amending it to perform actions as you want it to).
// when element with class 'subscribe-mailchimp' is clicked, execute function $('.subscribe-mailchimp').click(function(){ var $this = $(this); //get the value of the input field with ID 'email' inside the same parent element which contains the clicked 'subscribe-mailchimp' element data = $this.siblings('input#email').val(); if (data == '') { //no email entered - do what you want } else { $.getJSON('[URI FOR THE ABOVE SCRIPT - above.php]', {email: data}, function(data) { //console.log(data); if (data) { if (data.type) { //if error - do something //console.log('data'); } else if (data.id) { //if done - do something //console.log('data'); //console.log('done'); } else { //if other error - do something //console.log('data'); //console.log('else'); } } else { //if other error - do something //console.log('data'); //console.log('else'); } }) .fail(function() { //if failed to execute - do something //console.log('fail'); }); } })