*/ class Ebizmarts_MageMonkey_Model_MCEXPORTAPI extends Ebizmarts_MageMonkey_Model_MCAPI { var $version = "1.0"; /** * 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 __construct($apikey, $secure=false) { $this->secure = $secure; $this->apiUrl = parse_url("http://api.mailchimp.com/export/" . $this->version . "/"); $this->api_key = $apikey; } /** * @link http://apidocs.mailchimp.com/export/1.0/list.func.php */ public function listExport($id, $status = 'subscribed', $segment = NULL, $since = NULL) { $params = array(); $params["id"] = $id; $params["status"] = $status; if($segment) $params["segment"] = $segment; if($since) $params["since"] = $since; return $this->callServer("list", $params); } /** * Actually connect to the server and call the requested methods, parsing the result * You should never have to call this function manually */ function callServer($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"]; $params["apikey"] = $this->api_key; $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", "&"); } $post_vars = http_build_query($params); if ($sep_changed){ ini_set("arg_separator.output", $orig_sep); } $payload = "GET " . $this->apiUrl["path"] . $method . "/?" . $post_vars . " HTTP/1.0\r\n"; $payload .= "Host: " . $host . "\r\n"; $payload .= "User-Agent: " . Mage::helper('monkey')->getUserAgent() ."\r\n"; $payload .= "Connection: close \r\n\r\n"; 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); 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; } }