-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mailchimp.php
197 lines (165 loc) · 5.56 KB
/
Mailchimp.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace PYSys\Tools;
use Nette\Object;
use Nette\Utils\ArrayHash;
use Nette\Utils\Strings;
class Mailchimp extends Object
{
private $api_key;
private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0/';
private $verify_ssl = false;
const TYPE_GET = 0;
const TYPE_POST = 1;
const TYPE_PATCH = 2;
const MEMBER_SUBSCRIBED = "subscribed";
const MEMBER_UNSUBSCRIBED = "unsubscribed";
const MEMBER_PENDING = "pending";
const MEMBER_CLEANED = "cleaned";
const MEMBER_NOTEXISTS = "notexists";
/** @var ArrayHash|false */
public $last_result;
/**
* Create a new instance
*/
function __construct($api_key)
{
try {
$this->api_key = trim($api_key);
list(, $datacentre) = explode('-', $this->api_key);
$this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
} catch (\Exception $e) {
throw new MailchimpException("Unable to set API key");
}
}
/**
*
* Check, if email exist in list and return status
*
* @param $list
* @param $email
* @return string
* @throws MailchimpException
* @throws \Exception
*/
public function checkSubscriber($list, $email) {
if(empty($list)) {
throw new MailchimpException("Undefined Mailchimp list");
}
$email = Strings::lower($email);
try {
$this->call('lists/'.$list.'/members/' . md5($email), array(), self::TYPE_GET);
} catch (MailchimpException $e) {
if($e->getCode() != 404) {
throw $e;
}
return self::MEMBER_NOTEXISTS;
}
return $this->last_result->status;
}
/**
*
* Add or update email for subscribe newsletters
*
* @param $list
* @param $email
* @return bool
* @throws MailchimpException
* @throws \Exception
*/
public function addSubscriber($list, $email) {
if(empty($list)) {
throw new MailchimpException("Undefined Mailchimp list");
}
$email = Strings::lower($email);
$member_status = $this->checkSubscriber($list, $email);
if($member_status == self::MEMBER_NOTEXISTS) {
$result = $this->call('lists/'.$list.'/members/', array(
'email_address' => $email,
'status' => self::MEMBER_SUBSCRIBED
), self::TYPE_POST);
return ($result->status == self::MEMBER_SUBSCRIBED ? true : false);
} elseif ($member_status != self::MEMBER_SUBSCRIBED) {
$result = $this->call('lists/'.$list.'/members/' . md5($email), array(
"status" => self::MEMBER_SUBSCRIBED,
), self::TYPE_PATCH);
return ($result->status == self::MEMBER_SUBSCRIBED ? true : false);
}
return true;
}
/**
*
* Unsubscribe email from list
*
* @param $list
* @param $email
* @return bool
* @throws MailchimpException
* @throws \Exception
*/
public function delSubscriber($list, $email) {
if(empty($list)) {
throw new MailchimpException("Undefined Mailchimp list");
}
$email = Strings::lower($email);
$member_status = $this->checkSubscriber($list, $email);
if($member_status != self::MEMBER_UNSUBSCRIBED && $member_status != self::MEMBER_NOTEXISTS) {
$result = $this->call('lists/'.$list.'/members/' . md5($email), array(
"status" => self::MEMBER_UNSUBSCRIBED,
), self::TYPE_PATCH);
return ($result->status == self::MEMBER_UNSUBSCRIBED ? true : false);
}
return true;
}
/**
* Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in.
* @param string $method The API method to call, e.g. 'lists/list'
* @param array $args An array of arguments to pass to the method. Will be json-encoded for you.
* @return array Associative array of json decoded API response.
*/
public function call($method, $args=array(), $type=self::TYPE_POST)
{
return $this->_raw_request($method, $args, $type);
}
/**
* Performs the underlying HTTP request. Not very exciting
* @param string $method The API method to be called
* @param array $args Assoc array of parameters to be passed
* @param int $type Type of request
* @throws MailchimpException Errors
* @return ArrayHash Result
*/
private function _raw_request($method, $args=array(), $type=self::TYPE_POST)
{
$url = $this->api_endpoint . $method;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: apikey ' . $this->api_key
));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, ($type == self::TYPE_POST ? true : false));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
if($type != self::TYPE_GET) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
}
if($type == self::TYPE_PATCH) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
}
$result = curl_exec($ch);
curl_close($ch);
$this->last_result = ($result && json_decode($result) ? ArrayHash::from(json_decode($result)) : false);
if(!$this->last_result) {
throw new MailchimpException("Cannot connect to Mailchimp");
}
if(($type == self::TYPE_POST && !empty($this->last_result->code) && $this->last_result->code != 200) ||
($type == self::TYPE_POST && !empty($this->last_result->status) && $this->last_result->status != 200) ||
($type == self::TYPE_GET && !empty($this->last_result->status) && is_int($this->last_result->status) && $this->last_result->status != 200)) {
throw new MailchimpException($this->last_result->title . " | " . $this->last_result->detail, (!empty($this->last_result->code) ? $this->last_result->code : $this->last_result->status));
}
return $this->last_result;
}
}
class MailchimpException extends \Exception {}