· 5 years ago · Jun 22, 2020, 04:48 PM
1 $data = [
2 'email' => 'johndoe@example.com',
3 'status' => 'subscribed',
4 'firstname' => 'john',
5 'lastname' => 'doe'
6 ];
7
8 syncMailchimp($data);
9
10 function syncMailchimp($data) {
11 $apiKey = 'Tha api key';
12 $listId = 'The lis id';
13
14 $memberId = md5(strtolower($data['email']));
15 $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
16 $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
17
18 $json = json_encode([
19 'email_address' => $data['email'],
20 'status' => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
21 'merge_fields' => [
22 'FNAME' => $data['firstname'],
23 'LNAME' => $data['lastname']
24 ]
25 ]);
26
27 $ch = curl_init($url);
28
29 curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
30 curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
31 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
32 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
33 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
34 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
35 curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
36
37 $result = curl_exec($ch);
38 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
39 curl_close($ch);
40
41 return $httpCode;
42 }