You are here

function MCAPI::callServer in Mailchimp 6.2

Same name and namespace in other branches
  1. 5.2 MCAPI.class.php \MCAPI::callServer()
  2. 5 MCAPI.class.php \MCAPI::callServer()
  3. 6 MCAPI.class.php \MCAPI::callServer()
  4. 7 MCAPI.class.php \MCAPI::callServer()

Actually connect to the server and call the requested methods, parsing the result You should never have to call this function manually

80 calls to MCAPI::callServer()
MCAPI::apikeyAdd in ./MCAPI.class.php
Add an API Key to your account. We will generate a new key for you and return it.
MCAPI::apikeyExpire in ./MCAPI.class.php
Expire a Specific API Key. Note that if you expire all of your keys, just visit <a href="http://admin.mailchimp.com/account/api" target="_blank">your API dashboard</a> to create a new one. If you are trying to shut off…
MCAPI::apikeys in ./MCAPI.class.php
Retrieve a list of all MailChimp API Keys for this User
MCAPI::callMethod in ./MCAPI.class.php
Internal function - proxy method for certain XML-RPC calls | DO NOT CALL
MCAPI::campaignAbuseReports in ./MCAPI.class.php
Get all email addresses that complained about a given campaign

... See full list

File

./MCAPI.class.php, line 1947

Class

MCAPI

Code

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 = "";
  $post_vars = $this
    ->httpBuildQuery($params);
  $payload = "POST " . $this->apiUrl["path"] . "?" . $this->apiUrl["query"] . "&method=" . $method . " HTTP/1.0\r\n";
  $payload .= "Host: " . $host . "\r\n";
  $payload .= "User-Agent: MCAPI/" . $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) {
    try {
      $sock = @fsockopen("ssl://" . $host, 443, $errno, $errstr, 30);
      if (!$sock) {
        throw new Exception('Could not connect to Mailchimp');
      }
    } catch (Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'warning', $repeat = false);
      watchdog('mailchimp', 'MCAPI Error: %errormsg', array(
        '%errormsg' => $e
          ->getMessage(),
      ), WATCHDOG_ERROR);
    }
  }
  else {
    try {
      $sock = @fsockopen("ssl://" . $host, 443, $errno, $errstr, 30);
      if (!$sock) {
        throw new Exception('Could not connect to Mailchimp');
      }
    } catch (Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'warning', $repeat = false);
      watchdog('mailchimp', 'MCAPI Error: %errormsg', array(
        '%errormsg' => $e
          ->getMessage(),
      ), WATCHDOG_ERROR);
    }
  }
  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);
  }
  if ($info["timed_out"]) {
    $this->errorMessage = "Could not read response (timed out)";
    $this->errorCode = -98;
  }
  fclose($sock);
  ob_end_clean();
  if ($info["timed_out"]) {
    return false;
  }
  list($throw, $response) = explode("\r\n\r\n", $response, 2);
  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 (is_array($response) && isset($response["error"])) {
    $this->errorMessage = $response["error"];
    $this->errorCode = $response["code"];
    return false;
  }
  return $response;
}