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 $last_error = ''; |
||
25 | private $last_response = array(); |
||
26 | private $last_request = array(); |
||
27 | |||
28 | /** |
||
29 | * Create a new instance |
||
30 | * @param string $api_key Your MailChimp API key |
||
31 | */ |
||
32 | public function __construct($api_key) |
||
41 | |||
42 | /** |
||
43 | * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL |
||
44 | * @param string $email The subscriber's email address |
||
45 | * @return string Hashed version of the input |
||
46 | */ |
||
47 | public function subscriberHash($email) |
||
51 | |||
52 | /** |
||
53 | * Get the last error returned by either the network transport, or by the API. |
||
54 | * If something didn't work, this should contain the string describing the problem. |
||
55 | * @return array|false describing the error |
||
56 | */ |
||
57 | public function getLastError() |
||
62 | |||
63 | /** |
||
64 | * Get an array containing the HTTP headers and the body of the API response. |
||
65 | * @return array Assoc array with keys 'headers' and 'body' |
||
66 | */ |
||
67 | public function getLastResponse() |
||
71 | |||
72 | /** |
||
73 | * Get an array containing the HTTP headers and the body of the API request. |
||
74 | * @return array Assoc array |
||
75 | */ |
||
76 | public function getLastRequest() |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Make an HTTP DELETE request - for deleting data |
||
84 | * @param string URL of the API request method |
||
85 | * @param array Assoc array of arguments (if any) |
||
86 | * @param int Timeout limit for request in seconds |
||
87 | * @return array|false Assoc array of API response, decoded from JSON |
||
88 | */ |
||
89 | public function delete($method, $args=array(), $timeout=10) |
||
93 | |||
94 | /** |
||
95 | * Make an HTTP GET request - for retrieving data |
||
96 | * @param string URL of the API request method |
||
97 | * @param array Assoc array of arguments (usually your data) |
||
98 | * @param int Timeout limit for request in seconds |
||
99 | * @return array|false Assoc array of API response, decoded from JSON |
||
100 | */ |
||
101 | public function get($method, $args=array(), $timeout=10) |
||
105 | |||
106 | /** |
||
107 | * Make an HTTP PATCH request - for performing partial updates |
||
108 | * @param string URL of the API request method |
||
109 | * @param array Assoc array of arguments (usually your data) |
||
110 | * @param int Timeout limit for request in seconds |
||
111 | * @return array|false Assoc array of API response, decoded from JSON |
||
112 | */ |
||
113 | public function patch($method, $args=array(), $timeout=10) |
||
117 | |||
118 | /** |
||
119 | * Make an HTTP POST request - for creating and updating items |
||
120 | * @param string URL of the API request method |
||
121 | * @param array Assoc array of arguments (usually your data) |
||
122 | * @param int Timeout limit for request in seconds |
||
123 | * @return array|false Assoc array of API response, decoded from JSON |
||
124 | */ |
||
125 | public function post($method, $args=array(), $timeout=10) |
||
129 | |||
130 | /** |
||
131 | * Make an HTTP PUT request - for creating new items |
||
132 | * @param string URL of the API request method |
||
133 | * @param array Assoc array of arguments (usually your data) |
||
134 | * @param int Timeout limit for request in seconds |
||
135 | * @return array|false Assoc array of API response, decoded from JSON |
||
136 | */ |
||
137 | public function put($method, $args=array(), $timeout=10) |
||
141 | |||
142 | /** |
||
143 | * Performs the underlying HTTP request. Not very exciting |
||
144 | * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete |
||
145 | * @param string $method The API method to be called |
||
146 | * @param array $args Assoc array of parameters to be passed |
||
147 | * @return array|false Assoc array of decoded result |
||
148 | */ |
||
149 | private function makeRequest($http_verb, $method, $args=array(), $timeout=10) |
||
221 | |||
222 | /** |
||
223 | * Encode the data and attach it to the request |
||
224 | * @param resource cURL session handle, used by reference |
||
225 | * @param array Assoc array of data to attach |
||
226 | */ |
||
227 | private function attachRequestPayload(&$ch, $data) |
||
233 | |||
234 | private function formatResponse($response) |
||
251 | } |
||
252 |