-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc-endpoint.php
58 lines (43 loc) · 1.44 KB
/
mc-endpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
require_once(dirname(__FILE__).'../../../../wp-config.php');
//fill in these values for with your own information
$api_key = get_option('api_key');
$datacenter = explode('-', $api_key)[1];
$list_id = get_option('list_id');
$email = $_POST['EMAIL'];
if(get_option('opt_in') === "1"){
$status = 'pending';
} else {
$status = 'subscribed';
}
$url = 'https://'.$datacenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/';
$username = 'apikey';
$password = $api_key;
$data = array("email_address" => $email,"status" => $status);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$api_key");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result=curl_exec ($ch);
if($result !== "") {
$currentData = get_option('tma_subscribers');
$new_subscriber = array (
'email' => $email,
'date' => time()
);
if(!is_array($currentData)) {
$currentData = array();
}
array_push($currentData, $new_subscriber);
update_option( 'tma_subscribers', $currentData );
}
curl_close ($ch);
echo $result;