1 <?php
2 namespace MailChimp;
3
4 use GuzzleHttp\Client;
5 use GuzzleHttp\Exception\ClientException;
6 use MailChimp\AuthorizedApps\AuthorizedApps as AuthorizedApps;
7 use MailChimp\Automations\Automations as Automations;
8 use MailChimp\Batches\Batches as Batches;
9 use MailChimp\CampaignFolders\CampaignFolders as CampaignFolders;
10 use MailChimp\Campaigns\Campaigns as Campaigns;
11 use MailChimp\Conversations\Conversations as Conversations;
12 use MailChimp\Ecommerce\Ecommerce as Ecommerce;
13 use MailChimp\FileManager\Files as FileManagerFiles;
14 use MailChimp\FileManager\Folders as FileManagerFolders;
15 use MailChimp\Lists\Lists as Lists;
16 use MailChimp\Reports\Reports as Reports;
17 use MailChimp\TemplateFolders\TemplateFolders as TemplateFolders;
18 use MailChimp\Templates\Templates as Templates;
19
20
21 class MailChimp
22 {
23
24 private static $mc_root;
25 private static $api_key;
26 private static $config = "config.ini";
27 private static $client;
28
29 public function __construct()
30 {
31
32 $client = new Client([
33 'base_uri' => self::getUrl(),
34 'auth' => ['api', self::getActiveKey()],
35 'cookies' => true,
36 'allow_redirects' => true,
37 'http_errors' => false,
38 "headers" => [
39 "User-Agent" => "MCv3.0 / PHP",
40 "Accept" => "application/json"
41 ]
42 ]);
43 MailChimp::$client = $client;
44 }
45
46 47 48
49 private static function getUrl()
50 {
51 $dc = self::getDatacenter();
52 return "https://{$dc}.api.mailchimp.com/3.0/";
53 }
54
55 56 57
58 private static function getDatacenter()
59 {
60
61 $dc = trim(strstr(self::getActiveKey(), "-"), "-");
62 return $dc;
63 }
64
65 66 67
68 private static function getConfig()
69 {
70 $path_to_config = self::$config;
71 $config = parse_ini_file($path_to_config, true);
72 return $config;
73 }
74
75 76 77 78
79 private static function getActiveKey()
80 {
81 $config = self::getConfig();
82 foreach ($config["api_keys"] as $api) {
83 if ($api["active"]) {
84 return $api["api_key"];
85 break;
86 }
87 }
88 }
89
90 91 92 93 94 95
96 private static function setData($method, array $data = [])
97 {
98
99 foreach ($data as $key => $value) {
100
101 if ($method == "GET") {
102
103 if (is_array($value)) {
104 $value = implode(',', $value);
105 }
106
107 $params['query'][$key] = $value;
108 } else {
109 $params['json'][$key] = $value;
110 }
111 }
112 return $params;
113 }
114
115 protected static function execute($method, $url, array $data = [])
116 {
117 if ($data) {
118 $response = self::$client->request($method, $url, self::setData($method, $data));
119 } else {
120 $response = self::$client->request($method, $url);
121 }
122
123 $status_code = $response->getStatusCode();
124 $response_body = json_decode($response->getBody()->getContents());
125
126
127
128
129
130 return $response_body;
131 }
132
133 134 135 136 137 138
139 protected static function getMemberHash($email_address)
140 {
141 return md5(strtolower($email_address));
142 }
143
144 145 146 147 148 149
150 protected static function optionalFields(array $optional_fields, array $provided_fields)
151 {
152 $data = [];
153 foreach ($provided_fields as $key => $value) {
154 if (in_array(strtolower($key), $optional_fields) ) {
155 $data[$key] = $value;
156 }
157 }
158 return $data;
159 }
160
161 protected static function createLog($output, $overwrite = false, $file_name = "request.log", $tag = null)
162 {
163 $w = "a+";
164 if ($overwrite) {
165 $w = "w+";
166 }
167 $file = $file_name;
168 $json_output = json_encode($output);
169 $date = new \DateTime("now", new \DateTimeZone('America/New_York'));
170 $time_formatted = $date->format("Y/m/d H:i:s");
171 $handle = fopen($file, $w);
172 $content = "Request: {$time_formatted}\n";
173 if ($tag) {
174 $content .= "TAGGED: {$tag}";
175 $content .= "\n";
176 }
177 $content .= $json_output;
178 $content .= "\n";
179
180 $content .= "\n ---------------------------------------------------- \n";
181 fwrite($handle, $content);
182 fclose($handle);
183 }
184
185
186 public function logData($data, $tag, array $optional_settings = [])
187 {
188 if (isset($optional_settings["file_name"])) {
189 $file_name = $optional_settings["file_name"];
190 } else {
191 $file_name = null;
192 }
193
194 if (isset($optional_settings["overwrite"])) {
195 $overwrite = true;
196 } else {
197 $overwrite = false;
198 }
199
200 return self::createLog($data, $overwrite, $file_name, $tag);
201 }
202
203
204
205 206 207 208 209 210 211 212
213 public function getAccountInfo(array $query = [])
214 {
215 return self::execute("GET", "", $query);
216 }
217
218 219 220 221 222
223 public function searchMembers($query)
224 {
225 return self::execute("GET", "search-members", $query);
226 }
227
228
229 public function authorizedApps()
230 {
231 return new AuthorizedApps;
232 }
233
234 public function automations()
235 {
236 return new Automations;
237 }
238
239 public function batchOps()
240 {
241 return new Batches;
242 }
243
244 public function campaignFolders()
245 {
246 return new CampaignFolders;
247 }
248
249 public function campaigns()
250 {
251 return new Campaigns;
252 }
253
254 public function conversations()
255 {
256 return new Conversations;
257 }
258
259 public function ecommerce()
260 {
261 262 263
264 return new Ecommerce;
265 }
266
267 public function fileManager()
268 {
269 return new FileManager\Files;
270 }
271
272 public function fileManagerFolders()
273 {
274 return new FileManager\Folders;
275 }
276
277 public function lists()
278 {
279 return new Lists;
280 }
281
282 public function reports()
283 {
284 return new Reports;
285 }
286
287 public function templateFolders()
288 {
289 return new TemplateFolders;
290 }
291
292 public function templates()
293 {
294 295 296
297 return new Templates;
298 }
299
300 }
301