· 5 years ago · Oct 22, 2020, 01:50 PM
1<?php
2/*
3Name: Newsletter Subscribe
4Written by: Okler Themes - (http://www.okler.net)
5Theme Version: 8.1.0
6*/
7
8ini_set('allow_url_fopen', true);
9
10session_cache_limiter('nocache');
11header('Expires: ' . gmdate('r', 0));
12
13header('Content-type: application/json');
14
15include('./mailchimp/mailchimp.php');
16
17use \DrewM\MailChimp\MailChimp;
18
19// Step 1 - Set the apiKey - How get your Mailchimp API KEY - http://kb.mailchimp.com/article/where-can-i-find-my-api-key
20$apiKey = 'MAILCHIMP_API_KEY';
21
22// Step 2 - Set the listId - How to get your Mailchimp LIST ID - http://kb.mailchimp.com/article/how-can-i-find-my-list-id
23$listId = 'MAILCHIM_LIST_ID';
24
25
26if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
27
28 // Your Google reCAPTCHA generated Secret Key here
29 $secret = 'RECAPTCHA_SECRET_KEY';
30
31 if( ini_get('allow_url_fopen') ) {
32 //reCAPTCHA - Using file_get_contents()
33 $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
34 $responseData = json_decode($verifyResponse);
35 } else if( function_exists('curl_version') ) {
36 // reCAPTCHA - Using CURL
37 $fields = array(
38 'secret' => $secret,
39 'response' => $_POST['g-recaptcha-response'],
40 'remoteip' => $_SERVER['REMOTE_ADDR']
41 );
42
43 $verifyResponse = curl_init("https://www.google.com/recaptcha/api/siteverify");
44 curl_setopt($verifyResponse, CURLOPT_RETURNTRANSFER, true);
45 curl_setopt($verifyResponse, CURLOPT_TIMEOUT, 15);
46 curl_setopt($verifyResponse, CURLOPT_POSTFIELDS, http_build_query($fields));
47 $responseData = json_decode(curl_exec($verifyResponse));
48 curl_close($verifyResponse);
49 } else {
50 $arrResult = array ('response'=>'error','errorMessage'=>'You need CURL or file_get_contents() activated in your server. Please contact your host to activate.');
51 echo json_encode($arrResult);
52 die();
53 }
54
55 if($responseData->success) {
56
57 if (isset($_POST['newsletterEmail'])) {
58 $email = $_POST['newsletterEmail'];
59 } else if (isset($_GET['newsletterEmail'])) {
60 $email = $_GET['newsletterEmail'];
61 } else {
62 $email = '';
63 }
64
65 $MailChimp = new MailChimp($apiKey);
66
67 $result = $MailChimp->post('lists/' . $listId . '/members', array(
68 'email_address' => $email,
69 'merge_fields' => array('FNAME'=>$_POST['newsletterFirstName'], 'LNAME'=>$_POST['newsletterLastName']), // Step 3 (Optional) - Vars - More Information - http://kb.mailchimp.com/merge-tags/using/getting-started-with-merge-tags
70 'status' => 'subscribed'
71 ));
72
73 if ($result['id'] != '') {
74 $arrResult = array('response'=>'success');
75 } else {
76 $arrResult = array('response'=>'error','errorMessage'=>$result['detail']);
77 }
78
79 echo json_encode($arrResult);
80
81 } else {
82 $arrResult = array ('response'=>'error','errorMessage'=>'reCaptcha Error: Verifcation failed (no success). Please contact the website administrator.');
83 echo json_encode($arrResult);
84 }
85
86} else {
87 $arrResult = array ('response'=>'error','errorMessage'=>'reCaptcha Error: Invalid token. Please contact the website administrator.');
88 echo json_encode($arrResult);
89}