| 1 |
<?php |
| 2 |
/** |
| 3 |
This is a subset of the Actual MCAPI and miniMCAPI classes for v1.3 that includes the list related methods and support for PHP4 |
| 4 |
DOWNLOAD A NEW VERSION IF YOU WANT THE FULL, CORRECT WRAPPER. |
| 5 |
**/ |
| 6 |
class mailchimpSF_MCAPI { |
| 7 |
var $version = "1.3"; |
| 8 |
var $errorMessage; |
| 9 |
var $errorCode; |
| 10 |
|
| 11 |
/** |
| 12 |
* Cache the information on the API location on the server |
| 13 |
*/ |
| 14 |
var $apiUrl; |
| 15 |
|
| 16 |
/** |
| 17 |
* Default to a 300 second timeout on server calls |
| 18 |
*/ |
| 19 |
var $timeout = 300; |
| 20 |
|
| 21 |
/** |
| 22 |
* Default to a 8K chunk size |
| 23 |
*/ |
| 24 |
var $chunkSize = 8192; |
| 25 |
|
| 26 |
/** |
| 27 |
* Cache the user api_key so we only have to log in once per client instantiation |
| 28 |
*/ |
| 29 |
var $api_key; |
| 30 |
|
| 31 |
/** |
| 32 |
* Cache the user api_key so we only have to log in once per client instantiation |
| 33 |
*/ |
| 34 |
var $secure = false; |
| 35 |
|
| 36 |
/** |
| 37 |
* Connect to the MailChimp API for a given list. |
| 38 |
* |
| 39 |
* @param string $apikey Your MailChimp apikey |
| 40 |
* @param string $secure Whether or not this should use a secure connection |
| 41 |
*/ |
| 42 |
function mailchimpSF_MCAPI($apikey, $secure=false) { |
| 43 |
$this->secure = $secure; |
| 44 |
$this->apiUrl = parse_url("http://api.mailchimp.com/" . $this->version . "/?output=php"); |
| 45 |
$this->api_key = $apikey; |
| 46 |
} |
| 47 |
function setTimeout($seconds){ |
| 48 |
if (is_int($seconds)){ |
| 49 |
$this->timeout = $seconds; |
| 50 |
return true; |
| 51 |
} |
| 52 |
} |
| 53 |
function getTimeout(){ |
| 54 |
return $this->timeout; |
| 55 |
} |
| 56 |
function useSecure($val){ |
| 57 |
if ($val===true){ |
| 58 |
$this->secure = true; |
| 59 |
} else { |
| 60 |
$this->secure = false; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
function lists($filters=array ( |
| 65 |
), $start=0, $limit=25) { |
| 66 |
$params = array(); |
| 67 |
$params["filters"] = $filters; |
| 68 |
$params["start"] = $start; |
| 69 |
$params["limit"] = $limit; |
| 70 |
return $this->callServer("lists", $params); |
| 71 |
} |
| 72 |
|
| 73 |
function listMergeVars($id) { |
| 74 |
$params = array(); |
| 75 |
$params["id"] = $id; |
| 76 |
return $this->callServer("listMergeVars", $params); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
function listMergeVarAdd($id, $tag, $name, $options=array ( |
| 81 |
)) { |
| 82 |
$params = array(); |
| 83 |
$params["id"] = $id; |
| 84 |
$params["tag"] = $tag; |
| 85 |
$params["name"] = $name; |
| 86 |
$params["options"] = $options; |
| 87 |
return $this->callServer("listMergeVarAdd", $params); |
| 88 |
} |
| 89 |
|
| 90 |
function listMergeVarUpdate($id, $tag, $options) { |
| 91 |
$params = array(); |
| 92 |
$params["id"] = $id; |
| 93 |
$params["tag"] = $tag; |
| 94 |
$params["options"] = $options; |
| 95 |
return $this->callServer("listMergeVarUpdate", $params); |
| 96 |
} |
| 97 |
|
| 98 |
function listMergeVarDel($id, $tag) { |
| 99 |
$params = array(); |
| 100 |
$params["id"] = $id; |
| 101 |
$params["tag"] = $tag; |
| 102 |
return $this->callServer("listMergeVarDel", $params); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
function listInterestGroupings($id) { |
| 107 |
$params = array(); |
| 108 |
$params["id"] = $id; |
| 109 |
return $this->callServer("listInterestGroupings", $params); |
| 110 |
} |
| 111 |
|
| 112 |
function listInterestGroupAdd($id, $group_name, $grouping_id=NULL) { |
| 113 |
$params = array(); |
| 114 |
$params["id"] = $id; |
| 115 |
$params["group_name"] = $group_name; |
| 116 |
$params["grouping_id"] = $grouping_id; |
| 117 |
return $this->callServer("listInterestGroupAdd", $params); |
| 118 |
} |
| 119 |
|
| 120 |
function listInterestGroupDel($id, $group_name, $grouping_id=NULL) { |
| 121 |
$params = array(); |
| 122 |
$params["id"] = $id; |
| 123 |
$params["group_name"] = $group_name; |
| 124 |
$params["grouping_id"] = $grouping_id; |
| 125 |
return $this->callServer("listInterestGroupDel", $params); |
| 126 |
} |
| 127 |
|
| 128 |
function listInterestGroupUpdate($id, $old_name, $new_name, $grouping_id=NULL) { |
| 129 |
$params = array(); |
| 130 |
$params["id"] = $id; |
| 131 |
$params["old_name"] = $old_name; |
| 132 |
$params["new_name"] = $new_name; |
| 133 |
$params["grouping_id"] = $grouping_id; |
| 134 |
return $this->callServer("listInterestGroupUpdate", $params); |
| 135 |
} |
| 136 |
|
| 137 |
function listInterestGroupingAdd($id, $name, $type, $groups) { |
| 138 |
$params = array(); |
| 139 |
$params["id"] = $id; |
| 140 |
$params["name"] = $name; |
| 141 |
$params["type"] = $type; |
| 142 |
$params["groups"] = $groups; |
| 143 |
return $this->callServer("listInterestGroupingAdd", $params); |
| 144 |
} |
| 145 |
|
| 146 |
function listInterestGroupingUpdate($grouping_id, $name, $value) { |
| 147 |
$params = array(); |
| 148 |
$params["grouping_id"] = $grouping_id; |
| 149 |
$params["name"] = $name; |
| 150 |
$params["value"] = $value; |
| 151 |
return $this->callServer("listInterestGroupingUpdate", $params); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
function listInterestGroupingDel($grouping_id) { |
| 156 |
$params = array(); |
| 157 |
$params["grouping_id"] = $grouping_id; |
| 158 |
return $this->callServer("listInterestGroupingDel", $params); |
| 159 |
} |
| 160 |
|
| 161 |
function listSubscribe($id, $email_address, $merge_vars=NULL, $email_type='html', $double_optin=true, $update_existing=false, $replace_interests=true, $send_welcome=false) { |
| 162 |
$params = array(); |
| 163 |
$params["id"] = $id; |
| 164 |
$params["email_address"] = $email_address; |
| 165 |
$params["merge_vars"] = $merge_vars; |
| 166 |
$params["email_type"] = $email_type; |
| 167 |
$params["double_optin"] = $double_optin; |
| 168 |
$params["update_existing"] = $update_existing; |
| 169 |
$params["replace_interests"] = $replace_interests; |
| 170 |
$params["send_welcome"] = $send_welcome; |
| 171 |
return $this->callServer("listSubscribe", $params); |
| 172 |
} |
| 173 |
|
| 174 |
function listUnsubscribe($id, $email_address, $delete_member=false, $send_goodbye=true, $send_notify=true) { |
| 175 |
$params = array(); |
| 176 |
$params["id"] = $id; |
| 177 |
$params["email_address"] = $email_address; |
| 178 |
$params["delete_member"] = $delete_member; |
| 179 |
$params["send_goodbye"] = $send_goodbye; |
| 180 |
$params["send_notify"] = $send_notify; |
| 181 |
return $this->callServer("listUnsubscribe", $params); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
function listUpdateMember($id, $email_address, $merge_vars, $email_type='', $replace_interests=true) { |
| 186 |
$params = array(); |
| 187 |
$params["id"] = $id; |
| 188 |
$params["email_address"] = $email_address; |
| 189 |
$params["merge_vars"] = $merge_vars; |
| 190 |
$params["email_type"] = $email_type; |
| 191 |
$params["replace_interests"] = $replace_interests; |
| 192 |
return $this->callServer("listUpdateMember", $params); |
| 193 |
} |
| 194 |
|
| 195 |
function listBatchSubscribe($id, $batch, $double_optin=true, $update_existing=false, $replace_interests=true) { |
| 196 |
$params = array(); |
| 197 |
$params["id"] = $id; |
| 198 |
$params["batch"] = $batch; |
| 199 |
$params["double_optin"] = $double_optin; |
| 200 |
$params["update_existing"] = $update_existing; |
| 201 |
$params["replace_interests"] = $replace_interests; |
| 202 |
return $this->callServer("listBatchSubscribe", $params); |
| 203 |
} |
| 204 |
|
| 205 |
function listBatchUnsubscribe($id, $emails, $delete_member=false, $send_goodbye=true, $send_notify=false) { |
| 206 |
$params = array(); |
| 207 |
$params["id"] = $id; |
| 208 |
$params["emails"] = $emails; |
| 209 |
$params["delete_member"] = $delete_member; |
| 210 |
$params["send_goodbye"] = $send_goodbye; |
| 211 |
$params["send_notify"] = $send_notify; |
| 212 |
return $this->callServer("listBatchUnsubscribe", $params); |
| 213 |
} |
| 214 |
|
| 215 |
function listMembers($id, $status='subscribed', $since=NULL, $start=0, $limit=100) { |
| 216 |
$params = array(); |
| 217 |
$params["id"] = $id; |
| 218 |
$params["status"] = $status; |
| 219 |
$params["since"] = $since; |
| 220 |
$params["start"] = $start; |
| 221 |
$params["limit"] = $limit; |
| 222 |
return $this->callServer("listMembers", $params); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
function listMemberInfo($id, $email_address) { |
| 227 |
$params = array(); |
| 228 |
$params["id"] = $id; |
| 229 |
$params["email_address"] = $email_address; |
| 230 |
return $this->callServer("listMemberInfo", $params); |
| 231 |
} |
| 232 |
|
| 233 |
function listMemberActivity($id, $email_address) { |
| 234 |
$params = array(); |
| 235 |
$params["id"] = $id; |
| 236 |
$params["email_address"] = $email_address; |
| 237 |
return $this->callServer("listMemberActivity", $params); |
| 238 |
} |
| 239 |
|
| 240 |
function getAccountDetails() { |
| 241 |
$params = array(); |
| 242 |
return $this->callServer("getAccountDetails", $params); |
| 243 |
} |
| 244 |
|
| 245 |
function listsForEmail($email_address) { |
| 246 |
$params = array(); |
| 247 |
$params["email_address"] = $email_address; |
| 248 |
return $this->callServer("listsForEmail", $params); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
function campaignsForEmail($email_address) { |
| 253 |
$params = array(); |
| 254 |
$params["email_address"] = $email_address; |
| 255 |
return $this->callServer("campaignsForEmail", $params); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* "Ping" the MailChimp API - a simple method you can call that will return a constant value as long as everything is good. Note |
| 261 |
* than unlike most all of our methods, we don't throw an Exception if we are having issues. You will simply receive a different |
| 262 |
* string back that will explain our view on what is going on. |
| 263 |
* |
| 264 |
* @section Helper |
| 265 |
* @example xml-rpc_ping.php |
| 266 |
* |
| 267 |
* @return string returns "Everything's Chimpy!" if everything is chimpy, otherwise returns an error message |
| 268 |
*/ |
| 269 |
function ping() { |
| 270 |
$params = array(); |
| 271 |
return $this->callServer("ping", $params); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Internal function - proxy method for certain XML-RPC calls | DO NOT CALL |
| 276 |
* @param mixed Method to call, with any parameters to pass along |
| 277 |
* @return mixed the result of the call |
| 278 |
*/ |
| 279 |
function callMethod() { |
| 280 |
$params = array(); |
| 281 |
return $this->callServer("callMethod", $params); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Actually connect to the server and call the requested methods, parsing the result |
| 286 |
* You should never have to call this function manually |
| 287 |
*/ |
| 288 |
function callServer($method, $params) { |
| 289 |
$dc = "us1"; |
| 290 |
if (strstr($this->api_key,"-")){ |
| 291 |
list($key, $dc) = explode("-",$this->api_key,2); |
| 292 |
if (!$dc) $dc = "us1"; |
| 293 |
} |
| 294 |
$host = $dc.".".$this->apiUrl["host"]; |
| 295 |
$params["apikey"] = $this->api_key; |
| 296 |
|
| 297 |
$this->errorMessage = ""; |
| 298 |
$this->errorCode = ""; |
| 299 |
$sep_changed = false; |
| 300 |
//sigh, apparently some distribs change this to & by default |
| 301 |
if (ini_get("arg_separator.output")!="&"){ |
| 302 |
$sep_changed = true; |
| 303 |
$orig_sep = ini_get("arg_separator.output"); |
| 304 |
ini_set("arg_separator.output", "&"); |
| 305 |
} |
| 306 |
$post_vars = $this->httpBuildQuery($params); |
| 307 |
if ($sep_changed){ |
| 308 |
ini_set("arg_separator.output", $orig_sep); |
| 309 |
} |
| 310 |
|
| 311 |
$payload = "POST " . $this->apiUrl["path"] . "?" . $this->apiUrl["query"] . "&method=" . $method . " HTTP/1.0\r\n"; |
| 312 |
$payload .= "Host: " . $host . "\r\n"; |
| 313 |
$payload .= "User-Agent: MCAPIwp/" . $this->version ."\r\n"; |
| 314 |
$payload .= "Content-type: application/x-www-form-urlencoded\r\n"; |
| 315 |
$payload .= "Content-length: " . strlen($post_vars) . "\r\n"; |
| 316 |
$payload .= "Connection: close \r\n\r\n"; |
| 317 |
$payload .= $post_vars; |
| 318 |
|
| 319 |
ob_start(); |
| 320 |
if ($this->secure){ |
| 321 |
$sock = @fsockopen("ssl://".$host, 443, $errno, $errstr, 30); |
| 322 |
} else { |
| 323 |
$sock = @fsockopen($host, 80, $errno, $errstr, 30); |
| 324 |
} |
| 325 |
if(!$sock) { |
| 326 |
$this->errorMessage = "Could not connect (ERR $errno: $errstr)"; |
| 327 |
$this->errorCode = "-99"; |
| 328 |
ob_end_clean(); |
| 329 |
return false; |
| 330 |
} |
| 331 |
|
| 332 |
$response = ""; |
| 333 |
fwrite($sock, $payload); |
| 334 |
stream_set_timeout($sock, $this->timeout); |
| 335 |
$info = stream_get_meta_data($sock); |
| 336 |
while ((!feof($sock)) && (!$info["timed_out"])) { |
| 337 |
$response .= fread($sock, $this->chunkSize); |
| 338 |
$info = stream_get_meta_data($sock); |
| 339 |
} |
| 340 |
fclose($sock); |
| 341 |
ob_end_clean(); |
| 342 |
if ($info["timed_out"]) { |
| 343 |
$this->errorMessage = "Could not read response (timed out)"; |
| 344 |
$this->errorCode = -98; |
| 345 |
return false; |
| 346 |
} |
| 347 |
|
| 348 |
list($headers, $response) = explode("\r\n\r\n", $response, 2); |
| 349 |
$headers = explode("\r\n", $headers); |
| 350 |
$errored = false; |
| 351 |
foreach($headers as $h){ |
| 352 |
if (substr($h,0,26)==="X-MailChimp-API-Error-Code"){ |
| 353 |
$errored = true; |
| 354 |
$error_code = trim(substr($h,27)); |
| 355 |
break; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
if(ini_get("magic_quotes_runtime")) $response = stripslashes($response); |
| 360 |
|
| 361 |
$serial = unserialize($response); |
| 362 |
if($response && $serial === false) { |
| 363 |
$response = array("error" => "Bad Response. Got This: " . $response, "code" => "-99"); |
| 364 |
} else { |
| 365 |
$response = $serial; |
| 366 |
} |
| 367 |
if($errored && is_array($response) && isset($response["error"])) { |
| 368 |
$this->errorMessage = $response["error"]; |
| 369 |
$this->errorCode = $response["code"]; |
| 370 |
return false; |
| 371 |
} elseif($errored){ |
| 372 |
$this->errorMessage = "No error message was found"; |
| 373 |
$this->errorCode = $error_code; |
| 374 |
return false; |
| 375 |
} |
| 376 |
|
| 377 |
return $response; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Re-implement http_build_query for systems that do not already have it |
| 382 |
*/ |
| 383 |
function httpBuildQuery($params, $key=null) { |
| 384 |
$ret = array(); |
| 385 |
|
| 386 |
foreach((array) $params as $name => $val) { |
| 387 |
$name = urlencode($name); |
| 388 |
if($key !== null) { |
| 389 |
$name = $key . "[" . $name . "]"; |
| 390 |
} |
| 391 |
|
| 392 |
if(is_array($val) || is_object($val)) { |
| 393 |
$ret[] = $this->httpBuildQuery($val, $name); |
| 394 |
} elseif($val !== null) { |
| 395 |
$ret[] = $name . "=" . urlencode($val); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
return implode("&", $ret); |
| 400 |
} |
| 401 |
|
| 402 |
} |
| 403 |
?> |
| 404 |
|