-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailchimpAPI.php
234 lines (192 loc) · 6.65 KB
/
MailchimpAPI.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
<?php
class MailchimpAPI {
// Properties
public $protocol = "https://";
public $url = 'api.mailchimp.com/3.0/';
public $auth ;
public $dataCenter;
public $curlConnection;
function __construct($apiKey) {
$this->apiKey = $apiKey;
$this->auth = base64_encode( 'user:'.$apiKey );
$this->dataCenter = substr($apiKey,strpos($apiKey,'-')+1).".";
}
/**
* Create list in Mailchimp
* This mehotd receive one parameter: data to create list (audience)
* Example:
* $data = array(
* 'name' => 'name3',
* 'contact' => array(
* 'company' => 'company name',
* 'address1' => 'calle 15',
* 'city' => 'Your City',
* 'state' => 'CO',
* 'zip' => 'zipcode',
* 'country' => 'Colombia'
* ),
* 'permission_reminder' => 'permission reminder message',
* 'campaign_defaults' => array(
* 'from_name' => 'name from',
* 'from_email' => 'correo@hotmail.com',
* 'subject' => '',
* 'language' => 'en'
* ),
* 'email_type_option' => true
* );
*/
public function createList($data) {
$data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->protocol.$this->dataCenter.$this->url.'lists/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$this->auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.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, $data);
$result = curl_exec($ch);
//return $result;
if($result === false) {
$error = true;
$message = 'Error Mailchimp API add_list: '. curl_error($ch);
curl_close($ch);
} else {
$message = 'List added succesfully ';
curl_close($ch);
}
$response = array(
'result' => $result,
'message' => $message
);
return $response;
}
/**
* Request to delete list (audience)
*/
public function deleteList($listId) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->protocol.$this->dataCenter.$this->url.'lists/'.$listId);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$this->auth));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if($result === false) {
$error = true;
$message = 'Error Mailchimp API remove_list: '. curl_error($ch);
curl_close($ch);
} else {
$message = 'List removed succesfully ';
curl_close($ch);
}
$response = array(
'result' => $result,
'message' => $message
);
return $response;
}
/**
* This method will return a table with the lists of Mailchimp (id, name, member count)
*/
public function getLists(){
/*Obtener id de la lista recien creada*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->protocol.$this->dataCenter.$this->url.'lists/'."?count=5000");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$this->auth));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if($result === false){
$error = true;
$message = 'Error Mailchimp API id_list: '. curl_error($ch);
echo $message;
} else {
$result = json_decode( $result );
if( !empty($result->lists) ) {
echo "Lists <br>";
echo '<table>';
foreach( $result->lists as $list ){
echo '<tr><td>' . $list->id . '</td><td>' . $list->name . ' </td><td>' . $list->stats->member_count . '</td></tr>';
}
echo '</table>';
} elseif ( is_int( $result->status ) ) {
// More information about errors http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
$error = true;
$message = 'Error Mailchimp API id_list: '. $result->title. ' - '.$result->detail;
echo $message;
}
}
curl_close($ch);
}
/**
* Set nested array of contacts and return it
*/
public function setContactsList($contacts, $listId) {
foreach ($contacts as $contact) {
// Validate email format
if (
!empty($contact["email_address"]) &&
filter_var($contact["email_address"], FILTER_VALIDATE_EMAIL)
) {
$data = array(
'apikey' => $this->apiKey,
'email_address' => $contact["email_address"],
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => utf8_encode($contact["name"]),
'LNAME' => utf8_encode($contact["last_name"])
)
);
$jsonBodyData = json_encode($data);
$finalData['operations'][] = array(
"method" => "POST",
"path" => "/lists/$listId/members/",
"body" => $jsonBodyData
);
}
}
return $finalData;
}
/**
* Request to send nested array with list of contacts to Mailchimp
*/
public function sendContactsList($finalData) {
$data = json_encode($finalData);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->protocol.$this->dataCenter.$this->url.'batches/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','charset=utf-8',
'Authorization: Basic '.$this->auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.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, $data);
$result = curl_exec($ch);
if($result === false)
{
$message = ' Curl error: ' . curl_error($ch);
curl_close($ch);
}
else
{
$message = 'Contact list sent succesfully ';
curl_close($ch);
}
$response = array(
'result' => $result,
'message' => $message
);
return $response;
}
}