mailchimp.php
3.54 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Super-simple, minimum abstraction MailChimp API v2 wrapper
* Class name was renamed from MailChimp to MailChimp_Divi to avoid conflicts with some plugins.
* The use of curl and file_get_content has been replaced with WordPress' HTTP API
*
* Contributors:
* Michael Minor <me@pixelbacon.com>
* Lorna Jane Mitchell, github.com/lornajane
*
* @author Drew McLellan <drew.mclellan@gmail.com>
* @version 1.1.1
*
* The MIT License (MIT)
*
* Copyright (c) 2013 Drew McLellan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
class MailChimp_Divi
{
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(), $timeout = 10)
{
return $this->makeRequest($method, $args, $timeout);
}
/**
* 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
* @param int $timeout Time allocated before timeout
* @return array Assoc array of decoded result
*/
private function makeRequest( $method, $args_body = array(), $timeout = 10 )
{
// Prepare argument
$args = array(
'timeout' => $timeout,
'sslverify' => $this->verify_ssl,
'body' => array(
'apikey' => $this->api_key,
),
);
// Merge $args_body into $args['body']
if ( ! empty( $args_body ) ) {
$args['body'] = array_merge( $args['body'], $args_body );
}
// Setup URL
$url = $this->api_endpoint.'/'.$method.'.json';
// Request to MailChimp API and get result
$result = wp_remote_post( $url, $args );
// Return result
return $result;
}
}