forked from drewm/mailchimp-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailChimp.class.php
74 lines (59 loc) · 2.05 KB
/
MailChimp.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Super-simple, minimum abstraction MailChimp API v2 wrapper
*
* Requires curl (I know, right?)
* This probably has more comments than code.
*
* @author Drew McLellan <[email protected]>
* @version 1.0
*/
class MailChimp
{
private $api_key;
private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0/';
private $verify_ssl = false;
/**
* Create a new instance
* @param string $api_key Your MailChimp API key
*/
function __construct($api_key)
{
$this->api_key = $api_key;
list(, $datacentre) = explode('-', $this->api_key);
$this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
}
/**
* Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in.
* @param string $method The API method to call, e.g. 'lists/list'
* @param array $args An array of arguments to pass to the method. Will be json-encoded for you.
* @return array Associative array of json decoded API response.
*/
public function call($method, $args=array())
{
return $this->_raw_request($method, $args);
}
/**
* Performs the underlying HTTP request. Not very exciting
* @param string $method The API method to be called
* @param array $args Assoc array of parameters to be passed
* @return array Assoc array of decoded result
*/
private function _raw_request($method, $args=array())
{
$args['apikey'] = $this->api_key;
$url = $this->api_endpoint.'/'.$method.'.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
$result = curl_exec($ch);
curl_close($ch);
return $result ? json_decode($result, true) : false;
}
}