<?php /* Redirect browser if its not a post */ if ($_SERVER['REQUEST_METHOD'] !== 'POST') { header('Location: ' . '/404'); exit; } $data = ['email' => $_POST['email'], 'status' => 'subscribed']; $api_data = $api_keys[0]; $response = syncMailchimp($data, $api_data['api_key'], $api_data['list_id']); print $response; function syncMailchimp($data, $api_key, $list_id) { $content_type = 'Content-Type: application/json'; $method = 'PUT'; $apiKey = $api_key; $listId = $list_id; $memberId = md5(strtolower($data['email'])); $dataCenter = substr($apiKey, strpos($apiKey, '-') + 1); $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId; $json = json_encode(['email_address' => $data['email'], 'status' => $data['status']]); $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey); curl_setopt($ch, CURLOPT_HTTPHEADER, [$content_type]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$data = json_decode(file_get_contents('php://input'), true); $data['status'] = 'subscribed'; function syncMailchimp($apiKey, $listId, $data) { $memberId = md5(strtolower($data['email'])); $dataCenter = substr($apiKey, strpos($apiKey, '-') + 1); $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId; $json = json_encode(['email_address' => $data['email'], 'status' => $data['status'], 'merge_fields' => $data]); $headers[] = 'Content-Type: application/json'; $headers[] = 'Content-Length: ' . strlen($json); $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:'******'status' => $httpCode); } //EXECUTE $ret = syncMailchimp($apiKey, $listId, $data); header('Content-Type: application/json'); http_response_code($ret['status'] === 0 ? 401 : $ret['status']); echo json_encode($ret); die;
<?php $data = ['email' => $_POST['email'], 'status' => 'subscribed', 'firstname' => 'Foo', 'lastname' => 'Subscriber']; syncMailchimp($data); function syncMailchimp($data) { $apiKey = '***YOUR_API_KEY***'; $listId = '***YOUR_LIST_ID***'; $memberId = md5(strtolower($data['email'])); $dataCenter = substr($apiKey, strpos($apiKey, '-') + 1); $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId; $json = json_encode(['email_address' => $data['email'], 'status' => $data['status'], 'merge_fields' => ['FNAME' => $data['firstname'], 'LNAME' => $data['lastname']]]); $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:'******'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; }