// how to use
$apikey = 'xxx';
$listId = 'xxx';
$mailChimp = new MailChimp($apikey);
$result = $mailChimp->subscribe($listId, array(
'email_address' => $email_address,
'status' => 'subscribed',
'merge_fields' => array(
'NAME' => $name,
'STORE' => $store,
'RECEIPTNO' => $receipt_no,
)
));
// class
class MailChimp
{
private $api_key;
private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0';
public $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 subscribe($listId, $args = array())
{
return $this->post("/lists/$listId/members", $args, $timeout=10);
}
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 $http_verb
* @param $method
* @param array $args
* @param int $timeout
* @return bool|mixed
* @throws Exception
*/
private function makeRequest($http_verb, $method, $args=array(), $timeout=10)
{
$url = $this->api_endpoint.'/'.$method;
$json_data = json_encode($args, JSON_FORCE_OBJECT);
if (function_exists('curl_init') && function_exists('curl_setopt')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/vnd.api+json',
'Content-Type: application/vnd.api+json',
));
curl_setopt($ch, CURLOPT_USERPWD, 'dummy:' . $this->api_key);
curl_setopt($ch, CURLOPT_USERAGENT, 'MailChimp-API/3.0');
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;
case 'put':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
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;
}
}
Hi,
ReplyDeleteWe know, sign-up forms are important for building your fan or reader base. MailChimp lets you create custom pop-up forms that integrate with Facebook fan and business pages, or you can use a template form. MailChimp also makes it easy for you to create targeted marketing campaigns for your customers.i would like to suggest for looking Email marketing platform for relevant link.
ReplyDelete