MailChimp
<?php
$subscrSuccess = false;
$subscrError = false;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if(!empty($_POST['email'])){
$subscrSuccess = syncMailchimp($_POST['email']) == 200;
if(!$subscrSuccess){
$subscrError = true;
}
}
}
if($subscrSuccess){
$modx->setPlaceholder('mailchimp.success','Подписка успешно оформлена!');
}
if($subscrError){
$modx->setPlaceholder('mailchimp.error','Произошла ошибка попробуйте еще раз');
}
function syncMailchimp($email)
{
$apiKey = 'Your API Key';
$listId = 'Your List ID';
$memberId = md5(strtolower($email));
$dataCenter = substr($apiKey, strpos($apiKey, '-') + 1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
$json = json_encode(array(
'email_address' => $email,
'status' => 'subscribed', // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => array(
'FNAME' => '',
'LNAME' => ''
)
));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpCode;
}
?>