· 6 years ago · Jan 06, 2020, 02:20 PM
1/* For newsletter sign up */
2function mailchimp_subscribe($email, $status, $list_id, $api_key) {
3 $data = array(
4 'apikey' => $api_key,
5 'email_address' => $email,
6 'status' => $status,
7 "merge_fields" => array(
8 "FNAME"=> ""
9 )
10 );
11
12 $ch = curl_init('https://SUBDOMAIN.api.mailchimp.com/3.0/lists/'.$list_id.'/members/');
13 curl_setopt_array($ch, array(
14 CURLOPT_POST => TRUE,
15 CURLOPT_RETURNTRANSFER => TRUE,
16 CURLOPT_HTTPHEADER => array(
17 'Authorization: apikey '.$api_key,
18 'Content-Type: application/json'
19 ),
20 CURLOPT_POSTFIELDS => json_encode($data)
21 ));
22 // Send the request
23 $response = curl_exec($ch);
24}
25
26
27
28if(isset($_POST['EMAIL'])){
29 $email = $_POST['EMAIL'];
30 $status = 'subscribed';
31 $list_id = 'LIST_ID';
32 $api_key = 'API_KEY';
33 $merge_fields = array();
34 mailchimp_subscribe($email, $status, $list_id, $api_key);
35}
36
37/* Steps
38
391. Find your mailchimp API key (https://createform.com/support/mailchimp-api) and then replace "API_KEY" with the key you've found.
402. Next find your email list you want to add to and find its ID. (This can be done by going into the settings of the list and scrolling down to "Unique id for audience". Replace LIST_ID with the id you've just found.
413. At the end of your api key it should say "us" then a number (e.g us3 or us20). Replace SUBDOMAIN with your us[number] so the url should look something like https://us3.api.mailchimp.com/3.0/lists/'.$list_id.'/members/.
42*/