-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailchimpComponent.php
475 lines (417 loc) · 14.1 KB
/
MailchimpComponent.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* This is Cakephp 2.x plugin. A Mailchimp API v3.0 wrapper in cakephp style. This is still in development mode (beta version).
* Users are welcome to contribute their code for this plugin.
*
* Usage : public $components = array(
'Mailchimp' => array('key' => '<your API Key>')
);
* For more details about API visit below url
*
* @link http://developer.mailchimp.com/documentation/mailchimp/guides/get-started-with-mailchimp-api-3/
* @author World Wen Technology
* @version 1.0.0
*/
App::uses('Component', 'Controller');
class MailchimpComponent extends Component {
/**
* Your Mail chimp API key
*
* @var string Your Mailchimp API key
*/
private $_apiKey;
/**
* Your mail chimp API connection URL
* For more details, visit
* @link http://developer.mailchimp.com/documentation/mailchimp/guides/get-started-with-mailchimp-api-3/#resources
*
* @var url|string
*/
private $_endpoint = 'https://<dc>.api.mailchimp.com/3.0';
/**
* Last response from API
*
* @var array Response array from API
*/
private $_lastResponse = array();
/**
* SSL Verification
*
* @var boolean True|False
*/
public $verify_ssl = true;
/**
* Was request success?
*
* @var boolean True|False
*/
private $request_successful = false;
private $last_error = '';
private $last_request = array();
/**
* Constructor
*
* @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
* @param array $settings Array of configuration settings.
*/
public function __construct(ComponentCollection $collection, $settings = array()) {
$this->_controller = $collection->getController();
parent::__construct($collection, $settings);
$this->_apiKey = $this->settings['key'];
if($this->_apiKey == ""){
throw new APIKeyMissingException(__d('cake_dev','Mailchimp API key is missing'));
}
if(strpos($this->_apiKey,'-') === false){
throw new APIKeyInvalidException(__d('cake_dev','Your Mailchimp API key is not valid'));
}
list(,$dataCenter) = explode('-',$this->_apiKey);
$this->_endpoint = str_replace('<dc>',$dataCenter,$this->_endpoint);
$this->_lastResponse = array('headers' => null,'body' => null);
}
/**
* Initilization function
*
* @param Controller $controller
*/
public function initialize(Controller $controller) {
$this->controller = $controller;
}
/**
* Componenet startup function. It makes sure that Component was called from contrller and it is valid Call
*
* @param Controller $controller Controller instance
*/
public function startup(Controller $controller){
$this->controller = $controller;
}
/**
* Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL
* @param string $email The subscriber's email address
* @return string Hashed version of the input
*/
public function subscriberHash($email)
{
return md5(strtolower($email));
}
/**
* Was the last request successful?
* @return bool True for success, false for failure
*/
public function success()
{
return $this->request_successful;
}
/**
* Get the last error returned by either the network transport, or by the API.
* If something didn't work, this should contain the string describing the problem.
* @return array|false describing the error
*/
public function getLastError()
{
return $this->last_error;
}
/**
* Get an array containing the HTTP headers and the body of the API response.
* @return array Assoc array with keys 'headers' and 'body'
*/
public function getLastResponse()
{
return $this->_lastResponse;
}
/**
* Get an array containing the HTTP headers and the body of the API request.
* @return array Assoc array
*/
public function getLastRequest()
{
return $this->last_request;
}
/**
* Get Newsletter Group List id from Mailchimp
*
* @link http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#read-get_lists
* @param string $newLetterName Name of newsletter of which Id you want
* @return varchar|boolean Id of newsletter
*/
public function getListId($newLetterName = ""){
$getList = $this->get('lists');
if(!empty($getList)){
foreach ($getList['lists'] as $key => $value){
if($value['name'] == $newLetterName){
$listId = $value['id'];
}
}
return isset($listId) ? $listId : false;
}
return false;
}
/**
* Get Intrest category Id from mail ching
* @link http://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#read-get_listsget_lists_list_id_interest_categories
* @param string $groupName Name of intrest category You want to get
* @param varchar $listId Id of newsletter list this intrest category belongs to
* @return varchar|boolean Id of intreset category or false
*/
public function getIntrestCategory($groupName = "",$listId){
$newLetterName = "Test Newsletter";
$getListIntrestCategory = $this->get('lists/'.$listId.'/interest-categories');
if(!empty($getListIntrestCategory) && !empty($getListIntrestCategory['categories'])){
foreach ($getListIntrestCategory['categories'] as $key => $value){
if($value['title'] == $groupName && $value['list_id'] == $listId){
$categoryId = $value['id'];
}
}
return isset($categoryId) ? $categoryId : false;
}
return false;
}
/**
* Get All Categories inside of Intrest Group
* @link http://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/interests/#read-get_lists_list_id_interest_categories_interest_category_id_interests
* @param varchar $listId Id of newsletter list
* @param varchar $categoryId Category Id
*/
public function getAllIntresetInCategory($listId,$categoryId){
$intrestList = $this->get('lists/'.$listId.'/interest-categories/'.$categoryId.'/interests');
if(!empty($intrestList) && isset($intrestList['interests']) && !empty($intrestList['interests'])){
$intrestArray = array();
foreach ($intrestList['interests'] as $key => $val){
if($val['category_id'] == $categoryId && $val['list_id'] == $listId){
$intrestArray[$val['id']] = $val['name'];
}
}
return isset($intrestArray) && !empty($intrestArray) ? $intrestArray : false;
}
return false;
}
/**
* Make an HTTP DELETE request - for deleting data
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (if any)
* @param int $timeout Timeout limit for request in seconds
* @return array|false Assoc array of API response, decoded from JSON
*/
public function delete($method, $args = array(), $timeout = 10)
{
return $this->initRequest('delete', $method, $args, $timeout);
}
/**
* Make HTTP GET request- for getting data
*
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
* @param int $timeout Timeout limit for request in seconds
* @return array|false Assoc array of API response,decoded from JSON
*/
public function get($method,$args = array(),$timeout = 10){
return $this->initRequest('get',$method,$args,$timeout);
}
/**
* Make an HTTP PATCH request - for performing partial updates
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
* @param int $timeout Timeout limit for request in seconds
* @return array|false Assoc array of API response, decoded from JSON
*/
public function patch($method, $args = array(), $timeout = 10)
{
return $this->initRequest('patch', $method, $args, $timeout);
}
/**
* Make an HTTP POST request - for creating and updating items
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
* @param int $timeout Timeout limit for request in seconds
* @return array|false Assoc array of API response, decoded from JSON
*/
public function post($method, $args = array(), $timeout = 10)
{
return $this->initRequest('post', $method, $args, $timeout);
}
/**
* Make an HTTP PUT request - for creating new items
* @param string $method URL of the API request method
* @param array $args Assoc array of arguments (usually your data)
* @param int $timeout Timeout limit for request in seconds
* @return array|false Assoc array of API response, decoded from JSON
*/
public function put($method, $args = array(), $timeout = 10)
{
return $this->initRequest('put', $method, $args, $timeout);
}
/**
* Function to send request to Mailchimp using cUrl.
*
* @param string $http_verb Type of HTTP request, POST or GET or PUT or DELETE
* @param string $method Method to use on Mailchimp, GET,PATCH,PUT or DELETE
* @param array $args Data array to passe to mailchimp
* @param int $timeout Request timeout time
* @return boolean Response from Mailchimp API
*/
private function initRequest($http_verb,$method,$args = array(),$timeout = 10){
if(!function_exists('curl_init') || !function_exists('curl_setopt')){
throw new cUrlException('cURL support is required, but can\'t be found.');
}
$url = $this->_endpoint . '/' . $method;
$this->last_error = "";
$this->request_successful = false;
$response = array('header' => null,$body = null);
$this->last_response = $response;
$this->last_response = array(
'method' => $http_verb,
'path' => $method,
'url' => $url,
'body' => '',
'timeout' => $timeout
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/vnd.api+json',
'Content-Type: application/vnd.api+json',
'Authorization: apikey ' . $this->_apiKey
));
curl_setopt($ch, CURLOPT_USERAGENT, 'BMMC Mail chimp API Component');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
switch ($http_verb){
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
$this->attachRequestPayload($ch, $args);
break;
case 'get':
$query = http_build_query($args);
curl_setopt($ch, CURLOPT_URL, $url . '?' . $query);
break;
case 'delete':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'patch':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
$this->attachRequestPayload($ch, $args);
break;
case 'put':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$this->attachRequestPayload($ch, $args);
break;
}
$response['body'] = curl_exec($ch);
$response['headers'] = curl_getinfo($ch);
if (isset($response['headers']['request_header'])) {
$this->last_request['headers'] = $response['headers']['request_header'];
}
if ($response['body'] === false) {
$this->last_error = curl_error($ch);
}
curl_close($ch);
return $this->parseResponse($response);
}
/**
* Encode the data and attach it to the request
* @param resource $ch cURL session handle, used by reference
* @param array $data Assoc array of data to attach
*/
private function attachRequestPayload(&$ch, $dataArray)
{
$encoded = json_encode($dataArray);
$this->last_request['body'] = $encoded;
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
}
/**
* Decode the response and parse any error messages for debugging
* @param array $response The response from the curl request
* @return array|false The JSON decoded into an array
*/
private function parseResponse($response)
{
$this->last_response = $response;
if (!empty($response['body'])) {
$d = json_decode($response['body'], true);
if (isset($d['status']) && $d['status'] != '200' && isset($d['detail'])) {
$this->last_error = sprintf('%d: %s', $d['status'], $d['detail']);
} else {
$this->request_successful = true;
}
return $d;
}
return false;
}
}
class MailchimpException extends CakeException{
}
class APIKeyMissingException extends MailchimpException {
/**
* Constructor
*
* @param string $message If no message is given 'API key is missing' will be the message
* @param integer $code Status code, defaults to 500
*/
public function __construct($message = null, $code = 500) {
if (empty($message)) {
$message = 'API key is missing';
}
parent::__construct($message, $code);
}
}
class APIKeyInvalidException extends MailchimpException {
/**
* Constructer
*
* @param string $message If no message is given, 'API key sis invalid' will be the message
* @param integer $code Status code, defaults to 500
*/
public function __construct($message = null, $code = 500){
if(empty($message)) {
$message = "API key is invalid";
}
parent::__construct($message,$code);
}
}
class APIEndpointMissingException extends MailchimpException {
/**
* Construct
*
* @param string $message If no message given,"API end point is missing" will be the message
* @param integer $code Status code, defaults to 500
*/
public function __construct($message, $code = 500){
if(empty($message)){
$message = "API endpoing is missing";
}
parent::__construct($message,$code);
}
}
class ConnectionMethodMissingException extends MailchimpException {
/**
* Construct
*
* @param string $message If no message given,"Connecion PATH is missing" will be the message
* @param integer $code Status code, defaults to 500
*/
public function __construct($message,$code = 500){
if(empty($message))
{
$message = "Connecion PATH is missing";
}
parent::__construct($message,$code);
}
}
class cUrlException extends CacheException {
/**
* Construct
*
* @param string $message If no message given,"cUrl is missing" will be the message
* @param integer $code Status code, defaults to 500
*/
public function __construct($message,$code = 500){
if(empty($message))
{
$message = "cUrl is missing";
}
parent::__construct($message,$code);
}
}