Changeset 1057547
- Timestamp:
- 12/31/2014 04:09:07 PM (11 years ago)
- Location:
- woocommerce-to-autoresponders-woar/trunk
- Files:
-
- 2 edited
-
MCAPI.class.php (modified) (1 diff)
-
cron_execute.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
woocommerce-to-autoresponders-woar/trunk/MCAPI.class.php
r1056997 r1057547 1 1 <?php 2 2 3 class MCAPI { 4 var $version = "1.3"; 5 var $errorMessage; 6 var $errorCode; 7 8 /** 9 * Cache the information on the API location on the server 10 */ 11 var $apiUrl; 12 13 /** 14 * Default to a 300 second timeout on server calls 15 */ 16 var $timeout = 300; 17 18 /** 19 * Default to a 8K chunk size 20 */ 21 var $chunkSize = 8192; 22 23 /** 24 * Cache the user api_key so we only have to log in once per client instantiation 25 */ 26 var $api_key; 3 class MailChimp 4 { 5 private $api_key; 6 private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0'; 7 private $verify_ssl = false; 27 8 28 9 /** 29 * Cache the user api_key so we only have to log in once per client instantiation 30 */ 31 var $secure = false; 32 10 * Create a new instance 11 * @param string $api_key Your MailChimp API key 12 */ 13 function __construct($api_key) 14 { 15 $this->api_key = $api_key; 16 list(, $datacentre) = explode('-', $this->api_key); 17 $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint); 18 } 19 33 20 /** 34 * Connect to the MailChimp API for a given list. 35 * 36 * @param string $apikey Your MailChimp apikey 37 * @param string $secure Whether or not this should use a secure connection 38 */ 39 function MCAPI($apikey, $secure=false) { 40 $this->secure = $secure; 41 $this->apiUrl = parse_url("http://api.mailchimp.com/" . $this->version . "/?output=php"); 42 $this->api_key = $apikey; 21 * Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in. 22 * @param string $method The API method to call, e.g. 'lists/list' 23 * @param array $args An array of arguments to pass to the method. Will be json-encoded for you. 24 * @return array Associative array of json decoded API response. 25 */ 26 public function call($method, $args=array(), $timeout = 10) 27 { 28 return $this->makeRequest($method, $args, $timeout); 43 29 } 44 function setTimeout($seconds){ 45 if (is_int($seconds)){ 46 $this->timeout = $seconds; 47 return true; 48 } 49 } 50 function getTimeout(){ 51 return $this->timeout; 52 } 53 function useSecure($val){ 54 if ($val===true){ 55 $this->secure = true; 30 31 /** 32 * Performs the underlying HTTP request. Not very exciting 33 * @param string $method The API method to be called 34 * @param array $args Assoc array of parameters to be passed 35 * @return array Assoc array of decoded result 36 */ 37 private function makeRequest($method, $args=array(), $timeout = 10) 38 { 39 $args['apikey'] = $this->api_key; 40 41 $url = $this->api_endpoint.'/'.$method.'.json'; 42 43 if (function_exists('curl_init') && function_exists('curl_setopt')){ 44 $ch = curl_init(); 45 curl_setopt($ch, CURLOPT_URL, $url); 46 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 47 curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0'); 48 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 49 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 50 curl_setopt($ch, CURLOPT_POST, true); 51 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl); 52 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args)); 53 $result = curl_exec($ch); 54 curl_close($ch); 56 55 } else { 57 $this->secure = false; 58 } 59 } 60 61 /** 62 * Actually connect to the server and call the requested methods, parsing the result 63 * You should never have to call this function manually 64 */ 65 function __call($method, $params) { 66 $dc = "us1"; 67 if (strstr($this->api_key,"-")){ 68 list($key, $dc) = explode("-",$this->api_key,2); 69 if (!$dc) $dc = "us1"; 70 } 71 $host = $dc.".".$this->apiUrl["host"]; 72 73 $this->errorMessage = ""; 74 $this->errorCode = ""; 75 $sep_changed = false; 76 //sigh, apparently some distribs change this to & by default 77 if (ini_get("arg_separator.output")!="&"){ 78 $sep_changed = true; 79 $orig_sep = ini_get("arg_separator.output"); 80 ini_set("arg_separator.output", "&"); 81 } 82 //mutate params 83 $mutate = array(); 84 $mutate["apikey"] = $this->api_key; 85 foreach($params as $k=>$v){ 86 $mutate[$this->function_map[$method][$k]] = $v; 87 } 88 $post_vars = http_build_query($mutate); 89 if ($sep_changed){ 90 ini_set("arg_separator.output", $orig_sep); 91 } 92 93 $payload = "POST " . $this->apiUrl["path"] . "?" . $this->apiUrl["query"] . "&method=" . $method . " HTTP/1.0\r\n"; 94 $payload .= "Host: " . $host . "\r\n"; 95 $payload .= "User-Agent: MCAPImini/" . $this->version ."\r\n"; 96 $payload .= "Content-type: application/x-www-form-urlencoded\r\n"; 97 $payload .= "Content-length: " . strlen($post_vars) . "\r\n"; 98 $payload .= "Connection: close \r\n\r\n"; 99 $payload .= $post_vars; 100 101 ob_start(); 102 if ($this->secure){ 103 $sock = fsockopen("ssl://".$host, 443, $errno, $errstr, 30); 104 } else { 105 $sock = fsockopen($host, 80, $errno, $errstr, 30); 106 } 107 if(!$sock) { 108 $this->errorMessage = "Could not connect (ERR $errno: $errstr)"; 109 $this->errorCode = "-99"; 110 ob_end_clean(); 111 return false; 112 } 113 114 $response = ""; 115 fwrite($sock, $payload); 116 stream_set_timeout($sock, $this->timeout); 117 $info = stream_get_meta_data($sock); 118 while ((!feof($sock)) && (!$info["timed_out"])) { 119 $response .= fread($sock, $this->chunkSize); 120 $info = stream_get_meta_data($sock); 121 } 122 fclose($sock); 123 ob_end_clean(); 124 if ($info["timed_out"]) { 125 $this->errorMessage = "Could not read response (timed out)"; 126 $this->errorCode = -98; 127 return false; 56 $json_data = json_encode($args); 57 $result = file_get_contents($url, null, stream_context_create(array( 58 'http' => array( 59 'protocol_version' => 1.1, 60 'user_agent' => 'PHP-MCAPI/2.0', 61 'method' => 'POST', 62 'header' => "Content-type: application/json\r\n". 63 "Connection: close\r\n" . 64 "Content-length: " . strlen($json_data) . "\r\n", 65 'content' => $json_data, 66 ), 67 ))); 128 68 } 129 69 130 list($headers, $response) = explode("\r\n\r\n", $response, 2); 131 $headers = explode("\r\n", $headers); 132 $errored = false; 133 foreach($headers as $h){ 134 if (substr($h,0,26)==="X-MailChimp-API-Error-Code"){ 135 $errored = true; 136 $error_code = trim(substr($h,27)); 137 break; 138 } 139 } 140 141 if(ini_get("magic_quotes_runtime")) $response = stripslashes($response); 142 143 $serial = unserialize($response); 144 if($response && $serial === false) { 145 $response = array("error" => "Bad Response. Got This: " . $response, "code" => "-99"); 146 } else { 147 $response = $serial; 148 } 149 if($errored && is_array($response) && isset($response["error"])) { 150 $this->errorMessage = $response["error"]; 151 $this->errorCode = $response["code"]; 152 return false; 153 } elseif($errored){ 154 $this->errorMessage = "No error message was found"; 155 $this->errorCode = $error_code; 156 return false; 157 } 158 159 return $response; 70 return $result ? json_decode($result, true) : false; 160 71 } 161 162 protected $function_map = array('campaignUnschedule'=>array("cid"),163 'campaignSchedule'=>array("cid","schedule_time","schedule_time_b"),164 'campaignResume'=>array("cid"),165 'campaignPause'=>array("cid"),166 'campaignSendNow'=>array("cid"),167 'campaignSendTest'=>array("cid","test_emails","send_type"),168 'campaignSegmentTest'=>array("list_id","options"),169 'campaignCreate'=>array("type","options","content","segment_opts","type_opts"),170 'campaignUpdate'=>array("cid","name","value"),171 'campaignReplicate'=>array("cid"),172 'campaignDelete'=>array("cid"),173 'campaigns'=>array("filters","start","limit"),174 'campaignStats'=>array("cid"),175 'campaignClickStats'=>array("cid"),176 'campaignEmailDomainPerformance'=>array("cid"),177 'campaignMembers'=>array("cid","status","start","limit"),178 'campaignHardBounces'=>array("cid","start","limit"),179 'campaignSoftBounces'=>array("cid","start","limit"),180 'campaignUnsubscribes'=>array("cid","start","limit"),181 'campaignAbuseReports'=>array("cid","since","start","limit"),182 'campaignAdvice'=>array("cid"),183 'campaignAnalytics'=>array("cid"),184 'campaignGeoOpens'=>array("cid"),185 'campaignGeoOpensForCountry'=>array("cid","code"),186 'campaignEepUrlStats'=>array("cid"),187 'campaignBounceMessage'=>array("cid","email"),188 'campaignBounceMessages'=>array("cid","start","limit","since"),189 'campaignEcommOrders'=>array("cid","start","limit","since"),190 'campaignShareReport'=>array("cid","opts"),191 'campaignContent'=>array("cid","for_archive"),192 'campaignTemplateContent'=>array("cid"),193 'campaignOpenedAIM'=>array("cid","start","limit"),194 'campaignNotOpenedAIM'=>array("cid","start","limit"),195 'campaignClickDetailAIM'=>array("cid","url","start","limit"),196 'campaignEmailStatsAIM'=>array("cid","email_address"),197 'campaignEmailStatsAIMAll'=>array("cid","start","limit"),198 'campaignEcommOrderAdd'=>array("order"),199 'lists'=>array("filters","start","limit"),200 'listMergeVars'=>array("id"),201 'listMergeVarAdd'=>array("id","tag","name","options"),202 'listMergeVarUpdate'=>array("id","tag","options"),203 'listMergeVarDel'=>array("id","tag"),204 'listInterestGroupings'=>array("id"),205 'listInterestGroupAdd'=>array("id","group_name","grouping_id"),206 'listInterestGroupDel'=>array("id","group_name","grouping_id"),207 'listInterestGroupUpdate'=>array("id","old_name","new_name","grouping_id"),208 'listInterestGroupingAdd'=>array("id","name","type","groups"),209 'listInterestGroupingUpdate'=>array("grouping_id","name","value"),210 'listInterestGroupingDel'=>array("grouping_id"),211 'listWebhooks'=>array("id"),212 'listWebhookAdd'=>array("id","url","actions","sources"),213 'listWebhookDel'=>array("id","url"),214 'listStaticSegments'=>array("id"),215 'listStaticSegmentAdd'=>array("id","name"),216 'listStaticSegmentReset'=>array("id","seg_id"),217 'listStaticSegmentDel'=>array("id","seg_id"),218 'listStaticSegmentMembersAdd'=>array("id","seg_id","batch"),219 'listStaticSegmentMembersDel'=>array("id","seg_id","batch"),220 'listSubscribe'=>array("id","email_address","merge_vars","email_type","double_optin","update_existing","replace_interests","send_welcome"),221 'listUnsubscribe'=>array("id","email_address","delete_member","send_goodbye","send_notify"),222 'listUpdateMember'=>array("id","email_address","merge_vars","email_type","replace_interests"),223 'listBatchSubscribe'=>array("id","batch","double_optin","update_existing","replace_interests"),224 'listBatchUnsubscribe'=>array("id","emails","delete_member","send_goodbye","send_notify"),225 'listMembers'=>array("id","status","since","start","limit"),226 'listMemberInfo'=>array("id","email_address"),227 'listMemberActivity'=>array("id","email_address"),228 'listAbuseReports'=>array("id","start","limit","since"),229 'listGrowthHistory'=>array("id"),230 'listActivity'=>array("id"),231 'listLocations'=>array("id"),232 'listClients'=>array("id"),233 'templates'=>array("types","category","inactives"),234 'templateInfo'=>array("tid","type"),235 'templateAdd'=>array("name","html"),236 'templateUpdate'=>array("id","values"),237 'templateDel'=>array("id"),238 'templateUndel'=>array("id"),239 'getAccountDetails'=>array(),240 'generateText'=>array("type","content"),241 'inlineCss'=>array("html","strip_css"),242 'folders'=>array("type"),243 'folderAdd'=>array("name","type"),244 'folderUpdate'=>array("fid","name","type"),245 'folderDel'=>array("fid","type"),246 'ecommOrders'=>array("start","limit","since"),247 'ecommOrderAdd'=>array("order"),248 'ecommOrderDel'=>array("store_id","order_id"),249 'listsForEmail'=>array("email_address"),250 'campaignsForEmail'=>array("email_address"),251 'chimpChatter'=>array(),252 'apikeys'=>array("username","password","expired"),253 'apikeyAdd'=>array("username","password"),254 'apikeyExpire'=>array("username","password"),255 'ping'=>array());256 257 72 } -
woocommerce-to-autoresponders-woar/trunk/cron_execute.php
r1056997 r1057547 92 92 /*if($yes>0){*/ 93 93 require_once('MCAPI.class.php'); 94 // same directory as store-address.php 95 $listId=get_option('hangout_Mailchimp_list_id'); 96 $apikey=get_option('hangout_Mailchimp_api_key'); 97 $apiUrl = 'http://api.mailchimp.com/1.3/'; 98 // grab an API Key from http://admin.mailchimp.com/account/api/ 99 100 $api = new MCAPI($apikey); 101 102 103 $merge_vars = Array( 104 'EMAIL' => $email_address, 105 'FNAME' => $billing_first_name, 106 'LNAME' => $billing_last_name 107 108 ); 109 110 try{ 111 $api->listSubscribe($listId, $email_address, $merge_vars ); 112 }catch (Exception $e) { 113 //echo 'Caught exception: ', $e->getMessage(), "\n"; 114 } 94 $listId=get_option('hangout_Mailchimp_list_id'); 95 $apikey=get_option('hangout_Mailchimp_api_key'); 96 $MailChimp = new MailChimp($apikey); 97 $result = $MailChimp->call('lists/subscribe', array( 98 'id' => $listId, 99 'email' => array('email'=>$email_address), 100 'merge_vars' => array('FNAME'=>$billing_first_name, 'LNAME'=>$billing_last_name), 101 'double_optin' => false, 102 'update_existing' => true, 103 'replace_interests' => false, 104 'send_welcome' => false, 105 )); 115 106 /*}*/ 116 107
Note: See TracChangeset
for help on using the changeset viewer.