-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-mailchimp-subscribe.php
More file actions
131 lines (131 loc) · 5.03 KB
/
class-mailchimp-subscribe.php
File metadata and controls
131 lines (131 loc) · 5.03 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* MailChimp subscribe, unsubscribe and get user datas via MailChimp API 3.0
*
* https://github.com/jootamas/mailchimp-subscribe
*/
class MailChimp {
private $dataCenter;
public function __construct(){
/**
* get data center from end of API key
*/
$this->dataCenter = substr(MAILCHIMP_API_KEY, (strpos(MAILCHIMP_API_KEY, '-') + 1));
}
/**
* Validate email address
* @param string $email
* @return true|false
*/
public function validateEmail($email){
return filter_var(trim($email), FILTER_VALIDATE_EMAIL);
}
/**
* @param string $email email address
* @param string $forName forname
* @param string $lastName lastname
* @return string 'address-invalid' the email address is invalid
* 'address-exists' address already exists on this list
* boolean true subscribe success
* object the return object contains error status code:
* https://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary
*/
public function subscribe($email, $forName = '', $lastName = ''){
if(!$this->validateEmail(trim($email))){
return array('status' => 'address-invalid');
}
if($this->getSubscriber(trim($email)) !== false){
return array('status' => 'address-exists');
}
$data['apikey'] = MAILCHIMP_API_KEY;
$data['email_address'] = trim($email);
$data['status'] = 'subscribed';
if(isset($forName)){
$data['merge_fields']['FNAME'] = $forName;
}
if(isset($lastName)){
$data['merge_fields']['LNAME'] = $lastName;
}
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$this->dataCenter.'.api.mailchimp.com/3.0/lists/'.MAILCHIMP_LIST_ID.'/members');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: apikey '.MAILCHIMP_API_KEY));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/1.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = json_decode(curl_exec($ch));
if($result->status == 'subscribed'){
return true;
} else {
return $result;
}
}
/**
* Subscribe user from the list
* @param string $email email address
* @return boolean true unsubscribe success
* object the return object contains error status code:
* https://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary
*/
public function unSubscribe($email){
if(!$this->validateEmail(trim($email))){
return array('status' => 'invalid-address');
}
if($this->getSubscriber(trim($email)) === false){
return array('status' => 'address-no-exists');
}
$data['apikey'] = MAILCHIMP_API_KEY;
$data['email_address'] = trim($email);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$this->dataCenter.'.api.mailchimp.com/3.0/lists/'.MAILCHIMP_LIST_ID.'/members/'.md5($email));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: apikey '.MAILCHIMP_API_KEY));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/1.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = json_decode(curl_exec($ch));
if(!$result){
return true;
} else {
return $result;
}
}
/**
* Get datas of subscriber
* @param string $email email address
* @return boolean false email address no exists on this list
* object the return object contains user datas
*/
public function getSubscriber($email){
if(!$this->validateEmail(trim($email))){
return array('status' => 'invalid-address');
}
$data['apikey'] = MAILCHIMP_API_KEY;
$data['email_address'] = trim($email);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$this->dataCenter.'.api.mailchimp.com/3.0/lists/'.MAILCHIMP_LIST_ID.'/members/'.md5($email));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: apikey '.MAILCHIMP_API_KEY));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/1.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = json_decode(curl_exec($ch));
if($result->status == 'subscribed'){
return $result;
} else {
return false;
}
}
}
?>