Overview
  • Namespace
  • Class

Namespaces

  • MailChimp
    • AuthorizedApps
    • Automations
    • Batches
    • CampaignFolders
    • Campaigns
    • Conversations
    • Ecommerce
    • FileManager
    • Lists
    • Reports
    • TemplateFolders
    • Templates

Classes

  • MailChimp\AuthorizedApps\AuthorizedApps
  • MailChimp\Automations\Automations
  • MailChimp\Batches\Batches
  • MailChimp\CampaignFolders\CampaignFolders
  • MailChimp\Campaigns\Campaigns
  • MailChimp\Campaigns\Content
  • MailChimp\Campaigns\Feedback
  • MailChimp\Conversations\Conversations
  • MailChimp\Ecommerce\Carts
  • MailChimp\Ecommerce\Customers
  • MailChimp\Ecommerce\Ecommerce
  • MailChimp\Ecommerce\Orders
  • MailChimp\Ecommerce\Products
  • MailChimp\FileManager\Files
  • MailChimp\FileManager\Folders
  • MailChimp\Lists\Interests
  • MailChimp\Lists\Lists
  • MailChimp\Lists\Members
  • MailChimp\Lists\MergeFields
  • MailChimp\Lists\Segments
  • MailChimp\Lists\SignupForms
  • MailChimp\Lists\Webhooks
  • MailChimp\MailChimp
  • MailChimp\Reports\Reports
  • MailChimp\TemplateFolders\TemplateFolders
  • MailChimp\Templates\Templates
  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         // Setting http_errors to false since guzzle explodes for anything not 200
 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      * Get the API URL to use
 48      */
 49     private static function getUrl()
 50     {
 51         $dc = self::getDatacenter();
 52         return  "https://{$dc}.api.mailchimp.com/3.0/";
 53     }
 54 
 55     /**
 56      * Get the Datacenter from a the set API key
 57      */
 58     private static function getDatacenter()
 59     {
 60         // Determine the Datacenter from the API Key
 61         $dc = trim(strstr(self::getActiveKey(), "-"), "-");
 62         return $dc;
 63     }
 64 
 65     /**
 66      * Get the config file from the name set in $config
 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      * Find the key to use from the "active" key.
 77      * TODO: Is this way unnessesary?
 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      * Set the data passed for GET query parameters or POST/PUT/PATCH data
 92      *
 93      * @param array $data
 94      * @return array
 95      */
 96     private static function setData($method, array $data = [])
 97     {
 98         // TODO: consider sanitizing incoming data?
 99         foreach ($data as $key => $value) {
100             // Set query parameters if method is GET
101             if ($method == "GET") {
102                 // If the value is an array convert it to a string
103                 if (is_array($value)) {
104                     $value = implode(',', $value);
105                 }
106                 // Set the query param to an associative array
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         // if ($status_code === 200 || $status_code === 204) {
126             // return $response_body;
127         // } else {
128 
129         // }
130         return $response_body;
131     }
132 
133     /**
134      * Create the member hash
135      *
136      * @param string email address
137      * @return string
138      */
139     protected static function getMemberHash($email_address)
140     {
141         return md5(strtolower($email_address));
142     }
143 
144     /**
145      * Process optional fields for POST requests
146      * @param array $optional_fields
147      * @param array $provided_fields
148      * @return array
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         // $content .= print_r($output, true)."\n  ----------------------------------------------------  \n";
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     /** RESOURCES */
204 
205     /**
206      * Get account information from the API Root
207      * Available query fields:
208      * array["fields"]              array       list of strings of response fields to return
209      * array["exclude_fields"]      array       list of strings of response fields to exclude (not to be used with "fields")
210      * @param array $query (See Above) OPTIONAL associative array of query parameters.
211      * @return object
212      */
213     public function getAccountInfo(array $query = [])
214     {
215         return self::execute("GET", "", $query);
216     }
217 
218     /**
219      * Search all lists for members matching query
220      * @param string $query
221      * @return array of objects
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          * TODO: Collection in progress
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          * TODO: Collection in progress
296          */
297         return new Templates;
298     }
299 
300 } // End MailChimp class
301 
API documentation generated by ApiGen