Completed
Push — master ( c85027...a9bf61 )
by Drew
01:34
created

MailChimp::new_batch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace DrewM\MailChimp;
4
5
/**
6
 * Super-simple, minimum abstraction MailChimp API v3 wrapper
7
 * MailChimp API v3: http://developer.mailchimp.com
8
 * This wrapper: https://github.com/drewm/mailchimp-api
9
 *
10
 * @author Drew McLellan <[email protected]>
11
 * @version 2.1
12
 */
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)
33
    {
34
        $this->api_key = $api_key;
35
36
        list(, $datacentre)  = explode('-', $this->api_key);
37
        $this->api_endpoint  = str_replace('<dc>', $datacentre, $this->api_endpoint);
38
39
        $this->last_response = array('headers'=>null, 'body'=>null);
40
    }
41
42
    /**
43
     * Create a new instance of a Batch request. Optionally with the ID of an existing batch.
44
     * @param string $batch_id  Optional ID of an existing batch, if you need to check its status for example.
45
     * @return Batch            New Batch object.
46
     */
47
    public function new_batch($batch_id=null)
48
    {
49
        return new Batch($this, $batch_id);
50
    }
51
52
    /**
53
     * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL
54
     * @param   string  $email  The subscriber's email address
55
     * @return  string          Hashed version of the input
56
     */
57
    public function subscriberHash($email)
58
    {
59
        return md5(strtolower($email));
60
    }
61
62
    /**
63
     * Get the last error returned by either the network transport, or by the API.
64
     * If something didn't work, this should contain the string describing the problem.
65
     * @return  array|false  describing the error
66
     */
67
    public function getLastError()
68
    {
69
        if ($this->last_error) return $this->last_error;
70
        return false;
71
    }
72
73
    /**
74
     * Get an array containing the HTTP headers and the body of the API response.
75
     * @return array  Assoc array with keys 'headers' and 'body'
76
     */
77
    public function getLastResponse()
78
    {
79
        return $this->last_response;
80
    }
81
82
    /**
83
     * Get an array containing the HTTP headers and the body of the API request.
84
     * @return array  Assoc array
85
     */
86
    public function getLastRequest()
87
    {
88
        return $this->last_request;
89
    }
90
    
91
    /**
92
     * Make an HTTP DELETE request - for deleting data
93
     * @param   string  $method  URL of the API request method
94
     * @param   array   $args    Assoc array of arguments (if any)
95
     * @param   int     $timeout Timeout limit for request in seconds
96
     * @return  array|false   Assoc array of API response, decoded from JSON
97
     */
98
    public function delete($method, $args=array(), $timeout=10)
99
    {
100
        return $this->makeRequest('delete', $method, $args, $timeout);
101
    }
102
103
    /**
104
     * Make an HTTP GET request - for retrieving data
105
     * @param   string  $method   URL of the API request method
106
     * @param   array   $args     Assoc array of arguments (usually your data)
107
     * @param   int     $timeout  Timeout limit for request in seconds
108
     * @return  array|false   Assoc array of API response, decoded from JSON
109
     */
110
    public function get($method, $args=array(), $timeout=10)
111
    {
112
        return $this->makeRequest('get', $method, $args, $timeout);
113
    }
114
115
    /**
116
     * Make an HTTP PATCH request - for performing partial updates
117
     * @param   string  $method  URL of the API request method
118
     * @param   array   $args    Assoc array of arguments (usually your data)
119
     * @param   int     $timeout Timeout limit for request in seconds
120
     * @return  array|false   Assoc array of API response, decoded from JSON
121
     */
122
    public function patch($method, $args=array(), $timeout=10)
123
    {
124
        return $this->makeRequest('patch', $method, $args, $timeout);
125
    }
126
127
    /**
128
     * Make an HTTP POST request - for creating and updating items
129
     * @param   string  $method  URL of the API request method
130
     * @param   array   $args    Assoc array of arguments (usually your data)
131
     * @param   int     $timeout Timeout limit for request in seconds
132
     * @return  array|false   Assoc array of API response, decoded from JSON
133
     */
134
    public function post($method, $args=array(), $timeout=10)
135
    {
136
        return $this->makeRequest('post', $method, $args, $timeout);
137
    }
138
139
    /**
140
     * Make an HTTP PUT request - for creating new items
141
     * @param   string  $method  URL of the API request method
142
     * @param   array   $args    Assoc array of arguments (usually your data)
143
     * @param   int     $timeout Timeout limit for request in seconds
144
     * @return  array|false   Assoc array of API response, decoded from JSON
145
     */
146
    public function put($method, $args=array(), $timeout=10)
147
    {
148
        return $this->makeRequest('put', $method, $args, $timeout);
149
    }
150
151
    /**
152
     * Performs the underlying HTTP request. Not very exciting.
153
     * @param  string $http_verb    The HTTP verb to use: get, post, put, patch, delete
154
     * @param  string $method       The API method to be called
155
     * @param  array  $args         Assoc array of parameters to be passed
156
     * @return array|false          Assoc array of decoded result
157
     */
158
    private function makeRequest($http_verb, $method, $args=array(), $timeout=10)
159
    {
160
        if (!function_exists('curl_init') || !function_exists('curl_setopt')) {
161
            throw new \Exception("cURL support is required, but can't be found.");
162
        }
163
164
        $url = $this->api_endpoint.'/'.$method;
165
166
        $this->last_error    = '';
167
        $response            = array('headers'=>null, 'body'=>null);
168
        $this->last_response = $response;
169
170
        $this->last_request  = array(
171
                                'method' => $http_verb,
172
                                'path'   => $method,
173
                                'url'    => $url,
174
                                'body'   => '',
175
                                'timeout'=> $timeout,
176
                                );
177
178
        $ch = curl_init();
179
        curl_setopt($ch, CURLOPT_URL, $url);
180
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.api+json',
181
                                                    'Content-Type: application/vnd.api+json',
182
                                                    'Authorization: apikey '.$this->api_key));
183
        curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)');
184
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
185
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
186
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
187
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
188
        curl_setopt($ch, CURLOPT_ENCODING, '');
189
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
190
191
        switch($http_verb) {
192
            case 'post':
193
                curl_setopt($ch, CURLOPT_POST, true);
194
                $this->attachRequestPayload($ch, $args);
195
                break;
196
197
            case 'get':
198
                $query = http_build_query($args);
199
                curl_setopt($ch, CURLOPT_URL, $url.'?'.$query);
200
                break;
201
202
            case 'delete':
203
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
204
                break;
205
206
            case 'patch':
207
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
208
                $this->attachRequestPayload($ch, $args);
209
                break;
210
            
211
            case 'put':
212
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
213
                $this->attachRequestPayload($ch, $args);
214
                break;
215
        }
216
217
        $response['body']    = curl_exec($ch);
218
        $response['headers'] = curl_getinfo($ch);
219
220
        $this->last_request['headers'] = $response['headers']['request_header'];
221
        
222
        if ($response['body'] === false) {
223
            $this->last_error = curl_error($ch);
224
        }
225
        
226
        curl_close($ch);
227
228
        return $this->formatResponse($response);
229
    }
230
231
    /**
232
     * Encode the data and attach it to the request
233
     * @param   resource  $ch    cURL session handle, used by reference
234
     * @param   array     $data  Assoc array of data to attach
235
     */
236
    private function attachRequestPayload(&$ch, $data)
237
    {
238
        $encoded = json_encode($data);
239
        $this->last_request['body'] = $encoded;
240
        curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded); 
241
    }
242
243
    /**
244
     * Decode the response and format any error messages for debugging
245
     * @param array $response The response from the curl request
246
     * @return array|false     The JSON decoded into an array
247
     */
248
    private function formatResponse($response)
249
    {
250
        $this->last_response = $response;
251
252
        if (!empty($response['body'])) {
253
254
            $d = json_decode($response['body'], true);
255
            
256
            if (isset($d['status']) && $d['status']!='200' && isset($d['detail'])) {
257
                $this->last_error = sprintf('%d: %s', $d['status'], $d['detail']);
258
            }
259
            
260
            return $d;
261
        }
262
263
        return false;
264
    }
265
}
266