Plugin Directory

Changeset 635004


Ignore:
Timestamp:
12/06/2012 05:45:33 PM (13 years ago)
Author:
dpereyra
Message:

Fixed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • dp-maintenance-mode-lite/trunk/mailchimp/miniMCAPI.class.php

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