php
<?php // require('vendor/mailchimp/mailchimp/src/Mailchimp.php'); // try { // $api = new MailChimp('2b5c9242e4c9920930312da2e21d4b90-us11'); // $result = $api->lists->subscribe('8ce980c941', // array( // "email" => 'emran@gmail.com', // "name" => 'Emran' // ), // null, 'html', false, false, false, false // ); // var_dump($result); // } catch (Exception $e) { // error_log ('Exception while attempting to send email to '); // } function rudr_mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME' => '','LNAME' => '') ){ $data = array( 'apikey' => $api_key, 'email_address' => $email, 'status' => $status, 'merge_fields' => $merge_fields ); $mch_api = curl_init(); // initialize cURL connection curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address']))); curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key ))); curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0'); curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT curl_setopt($mch_api, CURLOPT_TIMEOUT, 10); curl_setopt($mch_api, CURLOPT_POST, true); curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json $result = curl_exec($mch_api); return $result; } $email = $_POST['email']; $name = $_POST['name']; $status = 'subscribed'; // "subscribed" or "unsubscribed" or "cleaned" or "pending" $list_id = '8ce980c941'; // where to get it read above $api_key = '2b5c9242e4c9920930312da2e21d4b90-us11'; // where to get it read above $merge_fields = array('FNAME' => $name,'LNAME' => ''); rudr_mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields ); echo 'You are subscribed.';