Completed
Push — master ( 980cb3...c85027 )
by Drew
03:13
created

MailChimp::subscriberHash()   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.0.9
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
     * 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)
48
    {
49
        return md5(strtolower($email));
50
    }
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()
58
    {
59
        if ($this->last_error) return $this->last_error;
60
        return false;
61
    }
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()
68
    {
69
        return $this->last_response;
70
    }
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()
77
    {
78
        return $this->last_request;
79
    }
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)
90
    {
91
        return $this->makeRequest('delete', $method, $args, $timeout);
92
    }
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)
102
    {
103
        return $this->makeRequest('get', $method, $args, $timeout);
104
    }
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)
114
    {
115
        return $this->makeRequest('patch', $method, $args, $timeout);
116
    }
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)
126
    {
127
        return $this->makeRequest('post', $method, $args, $timeout);
128
    }
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)
138
    {
139
        return $this->makeRequest('put', $method, $args, $timeout);
140
    }
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)
150
    {
151
        if (!function_exists('curl_init') || !function_exists('curl_setopt')) {
152
            throw new \Exception("cURL support is required, but can't be found.");
153
        }
154
155
        $url = $this->api_endpoint.'/'.$method;
156
157
        $this->last_error    = '';
158
        $response            = array('headers'=>null, 'body'=>null);
159
        $this->last_response = $response;
160
161
        $this->last_request  = array(
162
                                'method' => $http_verb,
163
                                'path'   => $method,
164
                                'url'    => $url,
165
                                'body'   => '',
166
                                'timeout'=> $timeout,
167
                                );
168
169
        $ch = curl_init();
170
        curl_setopt($ch, CURLOPT_URL, $url);
171
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.api+json',
172
                                                    'Content-Type: application/vnd.api+json',
173
                                                    'Authorization: apikey '.$this->api_key));
174
        curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)');
175
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
176
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
177
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
178
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
179
        curl_setopt($ch, CURLOPT_ENCODING, '');
180
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
181
182
        switch($http_verb) {
183
            case 'post':
184
                curl_setopt($ch, CURLOPT_POST, true);
185
                $this->attachRequestPayload($ch, $args);
186
                break;
187
188
            case 'get':
189
                $query = http_build_query($args);
190
                curl_setopt($ch, CURLOPT_URL, $url.'?'.$query);
191
                break;
192
193
            case 'delete':
194
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
195
                break;
196
197
            case 'patch':
198
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
199
                $this->attachRequestPayload($ch, $args);
200
                break;
201
            
202
            case 'put':
203
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
204
                $this->attachRequestPayload($ch, $args);
205
                break;
206
        }
207
208
        $response['body']    = curl_exec($ch);
209
        $response['headers'] = curl_getinfo($ch);
210
211
        $this->last_request['headers'] = $response['headers']['request_header'];
212
        
213
        if ($response['body'] === false) {
214
            $this->last_error = curl_error($ch);
215
        }
216
        
217
        curl_close($ch);
218
219
        return $this->formatResponse($response);
220
    }
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)
228
    {
229
        $encoded = json_encode($data);
230
        $this->last_request['body'] = $encoded;
231
        curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded); 
232
    }
233
234
    private function formatResponse($response)
235
    {
236
        $this->last_response = $response;
237
238
        if (!empty($response['body'])) {
239
240
            $d = json_decode($response['body'], true);
241
            
242
            if (isset($d['status']) && $d['status']!='200' && isset($d['detail'])) {
243
                $this->last_error = sprintf('%d: %s', $d['status'], $d['detail']);
244
            }
245
            
246
            return $d;
247
        }
248
249
        return false;
250
    }
251
}
252