1 | <?php |
||
13 | class MailChimp |
||
14 | { |
||
15 | private $api_key; |
||
16 | private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0'; |
||
17 | |||
18 | /* SSL Verification |
||
19 | Read before disabling: |
||
20 | http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/ |
||
21 | */ |
||
22 | public $verify_ssl = true; |
||
23 | |||
24 | private $request_successful = false; |
||
25 | private $last_error = ''; |
||
26 | private $last_response = array(); |
||
27 | private $last_request = array(); |
||
28 | |||
29 | /** |
||
30 | * Create a new instance |
||
31 | * @param string $api_key Your MailChimp API key |
||
32 | * @throws \Exception |
||
33 | */ |
||
34 | public function __construct($api_key) |
||
47 | |||
48 | /** |
||
49 | * Create a new instance of a Batch request. Optionally with the ID of an existing batch. |
||
50 | * @param string $batch_id Optional ID of an existing batch, if you need to check its status for example. |
||
51 | * @return Batch New Batch object. |
||
52 | */ |
||
53 | public function new_batch($batch_id = null) |
||
57 | |||
58 | /** |
||
59 | * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL |
||
60 | * @param string $email The subscriber's email address |
||
61 | * @return string Hashed version of the input |
||
62 | */ |
||
63 | public function subscriberHash($email) |
||
67 | |||
68 | /** |
||
69 | * Was the last request successful? |
||
70 | * @return bool True for success, false for failure |
||
71 | */ |
||
72 | public function success() |
||
76 | |||
77 | /** |
||
78 | * Get the last error returned by either the network transport, or by the API. |
||
79 | * If something didn't work, this should contain the string describing the problem. |
||
80 | * @return array|false describing the error |
||
81 | */ |
||
82 | public function getLastError() |
||
86 | |||
87 | /** |
||
88 | * Get an array containing the HTTP headers and the body of the API response. |
||
89 | * @return array Assoc array with keys 'headers' and 'body' |
||
90 | */ |
||
91 | public function getLastResponse() |
||
95 | |||
96 | /** |
||
97 | * Get an array containing the HTTP headers and the body of the API request. |
||
98 | * @return array Assoc array |
||
99 | */ |
||
100 | public function getLastRequest() |
||
104 | |||
105 | /** |
||
106 | * Make an HTTP DELETE request - for deleting data |
||
107 | * @param string $method URL of the API request method |
||
108 | * @param array $args Assoc array of arguments (if any) |
||
109 | * @param int $timeout Timeout limit for request in seconds |
||
110 | * @return array|false Assoc array of API response, decoded from JSON |
||
111 | */ |
||
112 | public function delete($method, $args = array(), $timeout = 10) |
||
116 | |||
117 | /** |
||
118 | * Make an HTTP GET request - for retrieving data |
||
119 | * @param string $method URL of the API request method |
||
120 | * @param array $args Assoc array of arguments (usually your data) |
||
121 | * @param int $timeout Timeout limit for request in seconds |
||
122 | * @return array|false Assoc array of API response, decoded from JSON |
||
123 | */ |
||
124 | public function get($method, $args = array(), $timeout = 10) |
||
128 | |||
129 | /** |
||
130 | * Make an HTTP PATCH request - for performing partial updates |
||
131 | * @param string $method URL of the API request method |
||
132 | * @param array $args Assoc array of arguments (usually your data) |
||
133 | * @param int $timeout Timeout limit for request in seconds |
||
134 | * @return array|false Assoc array of API response, decoded from JSON |
||
135 | */ |
||
136 | public function patch($method, $args = array(), $timeout = 10) |
||
140 | |||
141 | /** |
||
142 | * Make an HTTP POST request - for creating and updating items |
||
143 | * @param string $method URL of the API request method |
||
144 | * @param array $args Assoc array of arguments (usually your data) |
||
145 | * @param int $timeout Timeout limit for request in seconds |
||
146 | * @return array|false Assoc array of API response, decoded from JSON |
||
147 | */ |
||
148 | public function post($method, $args = array(), $timeout = 10) |
||
152 | |||
153 | /** |
||
154 | * Make an HTTP PUT request - for creating new items |
||
155 | * @param string $method URL of the API request method |
||
156 | * @param array $args Assoc array of arguments (usually your data) |
||
157 | * @param int $timeout Timeout limit for request in seconds |
||
158 | * @return array|false Assoc array of API response, decoded from JSON |
||
159 | */ |
||
160 | public function put($method, $args = array(), $timeout = 10) |
||
164 | |||
165 | /** |
||
166 | * Performs the underlying HTTP request. Not very exciting. |
||
167 | * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete |
||
168 | * @param string $method The API method to be called |
||
169 | * @param array $args Assoc array of parameters to be passed |
||
170 | * @param int $timeout |
||
171 | * @return array|false Assoc array of decoded result |
||
172 | * @throws Exception |
||
173 | */ |
||
174 | private function makeRequest($http_verb, $method, $args = array(), $timeout = 10) |
||
251 | |||
252 | /** |
||
253 | * Encode the data and attach it to the request |
||
254 | * @param resource $ch cURL session handle, used by reference |
||
255 | * @param array $data Assoc array of data to attach |
||
256 | */ |
||
257 | private function attachRequestPayload(&$ch, $data) |
||
263 | |||
264 | /** |
||
265 | * Decode the response and format any error messages for debugging |
||
266 | * @param array $response The response from the curl request |
||
267 | * @return array|false The JSON decoded into an array |
||
268 | */ |
||
269 | private function formatResponse($response) |
||
288 | } |
||
289 |