-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMCSTSAPI.class.php
175 lines (156 loc) · 5.43 KB
/
MCSTSAPI.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
class MCSTSAPI {
var $version = "1.0";
var $errorMessage;
var $errorCode;
/**
* Cache the information on the API location on the server
*/
var $apiUrl;
/**
* Default to a 300 second timeout on server calls
*/
var $timeout = 300;
/**
* Default to a 8K chunk size
*/
var $chunkSize = 8192;
/**
* Cache the user api_key so we only have to log in once per client instantiation
*/
var $api_key;
/**
* Cache the user api_key so we only have to log in once per client instantiation
*/
var $secure = false;
/**
* Connect to the MailChimp API for a given list.
*
* @param string $apikey Your MailChimp apikey
* @param string $secure Whether or not this should use a secure connection
*/
function MCSTSAPI($apikey, $secure=false) {
$this->secure = $secure;
$this->apiUrl = parse_url("http://sts.mailchimp.com/" . $this->version . "/?output=php");
$this->api_key = $apikey;
}
function setTimeout($seconds){
if (is_int($seconds)){
$this->timeout = $seconds;
return true;
}
}
function getTimeout(){
return $this->timeout;
}
function useSecure($val){
if ($val===true){
$this->secure = true;
} else {
$this->secure = false;
}
}
/**
* Actually connect to the server and call the requested methods, parsing the result
* You should never have to call this function manually
*/
function __call($method, $params) {
$dc = "us1";
if (strstr($this->api_key,"-")){
list($key, $dc) = explode("-",$this->api_key,2);
if (!$dc) $dc = "us1";
}
$host = $dc.".".$this->apiUrl["host"];
$this->errorMessage = "";
$this->errorCode = "";
$sep_changed = false;
//sigh, apparently some distribs change this to & by default
if (ini_get("arg_separator.output")!="&"){
$sep_changed = true;
$orig_sep = ini_get("arg_separator.output");
ini_set("arg_separator.output", "&");
}
//mutate params
$mutate = array();
$mutate["apikey"] = $this->api_key;
foreach($params as $k=>$v){
$mutate[$this->function_map[$method][$k]] = $v;
}
$post_vars = http_build_query($mutate);
if ($sep_changed){
ini_set("arg_separator.output", $orig_sep);
}
$payload = "POST " . $this->apiUrl["path"] . "/" . $method . ".php/?" . $this->apiUrl["query"] . " HTTP/1.0\r\n";
$payload .= "Host: " . $host . "\r\n";
$payload .= "User-Agent: MCAPImini/" . $this->version ."\r\n";
$payload .= "Content-type: application/x-www-form-urlencoded\r\n";
$payload .= "Content-length: " . strlen($post_vars) . "\r\n";
$payload .= "Connection: close \r\n\r\n";
$payload .= $post_vars;
ob_start();
if ($this->secure){
$sock = fsockopen("ssl://".$host, 443, $errno, $errstr, 30);
} else {
$sock = fsockopen($host, 80, $errno, $errstr, 30);
}
if(!$sock) {
$this->errorMessage = "Could not connect (ERR $errno: $errstr)";
$this->errorCode = "-99";
ob_end_clean();
return false;
}
$response = "";
fwrite($sock, $payload);
stream_set_timeout($sock, $this->timeout);
$info = stream_get_meta_data($sock);
while ((!feof($sock)) && (!$info["timed_out"])) {
$response .= fread($sock, $this->chunkSize);
$info = stream_get_meta_data($sock);
}
fclose($sock);
ob_end_clean();
if ($info["timed_out"]) {
$this->errorMessage = "Could not read response (timed out)";
$this->errorCode = -98;
return false;
}
list($headers, $response) = explode("\r\n\r\n", $response, 2);
$headers = explode("\r\n", $headers);
$errored = false;
foreach($headers as $h){
if (substr($h,0,26)==="X-MailChimp-API-Error-Code"){
$errored = true;
$error_code = trim(substr($h,27));
break;
}
}
if(ini_get("magic_quotes_runtime")) $response = stripslashes($response);
$serial = unserialize($response);
if($response && $serial === false) {
$response = array("error" => "Bad Response. Got This: " . $response, "code" => "-99");
} else {
$response = $serial;
}
if($errored && is_array($response) && isset($response["error"])) {
$this->errorMessage = $response["error"];
$this->errorCode = $response["code"];
return false;
} elseif($errored){
$this->errorMessage = "No error message was found";
$this->errorCode = $error_code;
return false;
}
return $response;
}
protected $function_map = array('deleteVerifiedEmailAddress'=>array("email"),
'listVerifiedEmailAddresses'=>array(),
'verifyEmailAddress'=>array("email"),
'getSendStats'=>array("tag_id","since"),
'getTags'=>array(),
'getUrlStats'=>array("url_id","since"),
'getUrls'=>array(),
'sendEmail'=>array("message","track_opens","track_clicks","tags"),
'getSendQuota'=>array(),
'getSendStatistics'=>array());
}
?>