· 5 years ago · Oct 22, 2020, 12:28 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
10include('./mailchimp/mailchimp.php');
11
12use \DrewM\MailChimp\MailChimp;
13
14// Step 1 - Set the apiKey - How get your Mailchimp API KEY - http://kb.mailchimp.com/article/where-can-i-find-my-api-key
15$apiKey = 'MAILCHIMP_API_KEY';
16
17// Step 2 - Set the listId - How to get your Mailchimp LIST ID - http://kb.mailchimp.com/article/how-can-i-find-my-list-id
18$listId = 'MAILCHIM_LIST_ID';
19
20
21if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
22
23 // Your Google reCAPTCHA generated Secret Key here
24 $secret = 'RECAPTCHA_SECRET_KEY';
25
26 if( ini_get('allow_url_fopen') ) {
27 //reCAPTCHA - Using file_get_contents()
28 $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
29 $responseData = json_decode($verifyResponse);
30 } else if( function_exists('curl_version') ) {
31 // reCAPTCHA - Using CURL
32 $fields = array(
33 'secret' => $secret,
34 'response' => $_POST['g-recaptcha-response'],
35 'remoteip' => $_SERVER['REMOTE_ADDR']
36 );
37
38 $verifyResponse = curl_init("https://www.google.com/recaptcha/api/siteverify");
39 curl_setopt($verifyResponse, CURLOPT_RETURNTRANSFER, true);
40 curl_setopt($verifyResponse, CURLOPT_TIMEOUT, 15);
41 curl_setopt($verifyResponse, CURLOPT_POSTFIELDS, http_build_query($fields));
42 $responseData = json_decode(curl_exec($verifyResponse));
43 curl_close($verifyResponse);
44 } else {
45 $arrResult = array ('response'=>'error','errorMessage'=>'You need CURL or file_get_contents() activated in your server. Please contact your host to activate.');
46 echo json_encode($arrResult);
47 die();
48 }
49
50 if($responseData->success) {
51
52 if (isset($_POST['newsletterEmail'])) {
53 $email = $_POST['newsletterEmail'];
54 } else if (isset($_GET['newsletterEmail'])) {
55 $email = $_GET['newsletterEmail'];
56 } else {
57 $email = '';
58 }
59
60 $MailChimp = new MailChimp($apiKey);
61
62 $result = $MailChimp->post('lists/' . $listId . '/members', array(
63 'email_address' => $email,
64 '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
65 'status' => 'subscribed'
66 ));
67
68 if ($result['id'] != '') {
69 $arrResult = array('response'=>'success');
70 } else {
71 $arrResult = array('response'=>'error','message'=>$result['detail']);
72 }
73
74 echo json_encode($arrResult);
75
76 } else {
77 $arrResult = array ('response'=>'error','errorMessage'=>'reCaptcha Error: Verifcation failed (no success). Please contact the website administrator.');
78 echo json_encode($arrResult);
79 }
80
81} else {
82 $arrResult = array ('response'=>'error','errorMessage'=>'reCaptcha Error: Invalid token. Please contact the website administrator.');
83 echo json_encode($arrResult);
84}