@ 2015-11-01T12:34:16Z <?php
/**
* Super-simple, minimum abstraction MailChimp API v3 wrapper
*
* Uses curl if available, falls back to file_get_contents and HTTP stream.
* This probably has more comments than code.
*
* @author Drew McLellan <drew.mclellan@gmail.com>
* @version 2.0
*/
class MailChimp
{
private $api_key;
private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0';
private $verify_ssl = true;
/**
* Create a new instance
* @param string $api_key Your MailChimp API key
*/
public function __construct($api_key)
{
$this->api_key = $api_key;
list(, $datacentre) = explode('-', $this->api_key);
$this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
}
public function delete($method, $args=array(), $timeout=10)
{
return $this->makeRequest('delete', $method, $args, $timeout);
}
public function get($method, $args=array(), $timeout=10)
{
return $this->makeRequest('get', $method, $args, $timeout);
}
public function patch($method, $args=array(), $timeout=10)
{
return $this->makeRequest('patch', $method, $args, $timeout);
}
public function post($method, $args=array(), $timeout=10)
{
return $this->makeRequest('post', $method, $args, $timeout);
}
public function put($method, $args=array(), $timeout=10)
{
return $this->makeRequest('put', $method, $args, $timeout);
}
/**
* Performs the underlying HTTP request. Not very exciting
* @param string $$http_verb The HTTP verb to use: get, post, put, patch, delete
* @param string $method The API method to be called
* @param array $args Assoc array of parameters to be passed
* @return array Assoc array of decoded result
*/
private function makeRequest($http_verb, $method, $args=array(), $timeout=10)
{
$args['apikey'] = $this->api_key;
$url = $this->api_endpoint.'/'.$method;
$json_data = json_encode((object)$args);
if (function_exists('curl_init') && function_exists('curl_setopt')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'drewm:'.$this->api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.api+json',
'Content-Type: application/vnd.api+json'));
curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
switch($http_verb) {
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
break;
case 'get':
$query = http_build_query($args);
curl_setopt($ch, CURLOPT_URL, $url.'?'.$query);
break;
case 'delete':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'patch':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
break;
}
$result = curl_exec($ch);
curl_close($ch);
} else {
throw new Exception("cURL support is required, but can't be found.");
}
return $result ? json_decode($result, true) : false;
}
}
// MailChimp Configuration
$api_key = "f36426878616bc7a2e1fc918ab9f9de5-us12"; // ENTER YOUR API KEY HERE
$list_id = "d7973804f4"; // ENTER YOUR LIST ID HERE
$MailChimp = new MailChimp($api_key);
$result = $MailChimp->post('lists/' .$list_id. '/members', array(
'email_address' => 'rajan@gmail.com',
'status' => 'subscribed'
));
if($result['status'] == 'subscribed') {
echo json_encode(array('error' => false, 'message' => 'Thanks for subscribing with us'));
exit;
} else {
echo json_encode(array('error' => true, 'message' => $result['title']));
exit;
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: Uncaught Exception: cURL support is required, but can't be found. in /in/pXdJ5:109
Stack trace:
#0 /in/pXdJ5(47): MailChimp->makeRequest('post', 'lists/d7973804f...', Array, 10)
#1 /in/pXdJ5(121): MailChimp->post('lists/d7973804f...', Array)
#2 {main}
thrown in /in/pXdJ5 on line 109
Process exited with code 255 . Output for 7.0.0 - 7.0.20 , 7.1.0 - 7.1.25 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 Fatal error: Uncaught Exception: cURL support is required, but can't be found. in /in/pXdJ5:109
Stack trace:
#0 /in/pXdJ5(47): MailChimp->makeRequest('post', 'lists/d7973804f...', Array, 10)
#1 /in/pXdJ5(122): MailChimp->post('lists/d7973804f...', Array)
#2 {main}
thrown in /in/pXdJ5 on line 109
Process exited with code 255 . Output for 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.28 Fatal error: Uncaught exception 'Exception' with message 'cURL support is required, but can't be found.' in /in/pXdJ5:109
Stack trace:
#0 /in/pXdJ5(47): MailChimp->makeRequest('post', 'lists/d7973804f...', Array, 10)
#1 /in/pXdJ5(124): MailChimp->post('lists/d7973804f...', Array)
#2 {main}
thrown in /in/pXdJ5 on line 109
Process exited with code 255 . preferences:dark mode live preview
233.52 ms | 402 KiB | 282 Q