26 Dec 2016

Mailchimp Signle Opt In Api Code

Mailchimp Signle Opt In Api Code -

<?php
include_once('mailchimp/MailChimp.class.php');
$mailchimp_api="c562cb098c4170e2136eca81c3e4d9bb-us13";
$MailChimp = new MailChimp($mailchimp_api);


if(isset($_REQUEST['FLAG']) & $_REQUEST['FLAG']=="SEND_SUBSCRIBE")
{

$email = isset($_REQUEST['EMAIL'])?trim($_REQUEST['EMAIL']):'';
$fName = isset($_REQUEST['FNAME'])?trim($_REQUEST['FNAME']):'';
$lName = isset($_REQUEST['LNAME'])?trim($_REQUEST['LNAME']):'';

//print_r($email);
//exit;

if((!isset($email) || $email=='') && !filter_var( $email ,FILTER_VALIDATE_EMAIL)){
echo 'This email address is not valid';
}else{
$result = $MailChimp->call('lists/subscribe', array(
'id'                => '86654347f6',
'email'             => array('email'=>$email),
'merge_vars'        => array('MERGE1'=>$fName, 'MERGE2'=>$lName),
'double_optin'      => false,
'update_existing'   => true,
'replace_interests' => false,
'send_welcome'      => false,
));
  // echo 'success';
if (!empty( $result['leid']) ) {
  header("Location: ".$siteUrl."/newsletter-opt-in-confirmation/");
exit;
}
else
{
header("Location: ".$siteUrl."/newsletter-opt-in/?res=error");
exit;
}
}
}


<?php
/**
 * Super-simple, minimum abstraction MailChimp API v2 wrapper
 *
 * Requires curl (I know, right?)
 * This probably has more comments than code.
 *
 * @author Drew McLellan <drew.mclellan@gmail.com>
 * @version 1.0
 */
class MailChimp
{
private $api_key;
private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0/';
private $verify_ssl   = false;



/**
* Create a new instance
* @param string $api_key Your MailChimp API key
*/
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);
}




/**
* Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in.
* @param  string $method The API method to call, e.g. 'lists/list'
* @param  array  $args   An array of arguments to pass to the method. Will be json-encoded for you.
* @return array          Associative array of json decoded API response.
*/
public  function call($method, $args=array())
{
return $this->_raw_request($method, $args);
}




/**
* Performs the underlying HTTP request. Not very exciting
* @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 _raw_request($method, $args=array())
{    
$args['apikey'] = $this->api_key;

$url = $this->api_endpoint.'/'.$method.'.json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
$result = curl_exec($ch);
curl_close($ch);

return $result ? json_decode($result, true) : false;
}

}

No comments:

Post a Comment