Home > Uncategorized > Subscribe to mailchimp via API in PHP

Subscribe to mailchimp via API in PHP

This is a handy little PHP script using CURL that adds an email address onto a MailChimp list
taken from: https://apidocs.mailchimp.com/api/1.3/listsubscribe.func.php

<?php
$apiKey = ‘xxxxxx’; // your mailchimp API KEY here
$listId = ‘xxxxx’; // your mailchimp LIST ID here
$double_optin=false;
$send_welcome=false;
$email_type = ‘html’;
$email = $_POST[’email’];
//replace us2 with your actual datacenter
$submit_url = “http://us10.api.mailchimp.com/1.3/?method=listSubscribe&#8221;;
$data = array(
’email_address’=>$email,
‘apikey’=>$apiKey,
‘id’ => $listId,
‘double_optin’ => $double_optin,
‘send_welcome’ => $send_welcome,
’email_type’ => $email_type
);
$payload = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload));

$result = curl_exec($ch);
curl_close ($ch);
$data = json_decode($result);
if ($data->error){
echo $data->error;
} else {
echo ‘Got it, you\’ve been added to our email list.’;
}
?>

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: