You are here

public function MostPopularAddThis::fetch in Drupal Most Popular 7

Makes a generic call to the AddThis.com server.

See http://www.addthis.com/help/analytics-api

Parameters

string $dimension: The dimension we're interested in. It must be null or one of:

  • url
  • continent
  • country
  • domain
  • service

string $format: The format to return results, either 'csv' or 'json'.

string $params: Additional parameters to send. The following are allowed:

  • period: 'day', 'week' or 'month'
  • domain: any URL domain
  • service: 'email'
1 call to MostPopularAddThis::fetch()
MostPopularAddThis::fetchJson in modules/mostpopular_addthis/mostpopular_addthis.classes.inc
Makes a call to the AddThis.com server and parses the JSON response.

File

modules/mostpopular_addthis/mostpopular_addthis.classes.inc, line 59
Provides a connector to the AddThis.com API.

Class

MostPopularAddThis
@file Provides a connector to the AddThis.com API.

Code

public function fetch($dimension = '', $format = 'csv', $params = array()) {
  if (empty($this->username) || empty($this->password) || empty($this->pubid)) {
    drupal_set_message(t('You must configure the AddThis.com username, password, and publisher profile id.'), 'error');
    return FALSE;
  }

  // Create the URL
  $params += array(
    'userid' => $this->username,
    'password' => $this->password,
    'pubid' => $this->pubid,
    'domain' => $this->domain,
  );
  $url = self::data_url;
  if (!empty($dimension)) {
    $url .= '/' . $dimension;
  }
  $url .= '.' . $format;

  // Clean up the URL and add query parameters
  $request_url = url(self::data_scheme . $url, array(
    'query' => $params,
  ));
  $this->result = drupal_http_request($request_url);
  if (variable_get('mostpopular_debug', FALSE)) {
    watchdog('mostpopular_addthis', 'Request to !url<pre>!response</pre>', array(
      '!url' => $request_url,
      '!response' => print_r($this->result, TRUE),
    ), WATCHDOG_DEBUG);
  }
  if (isset($this->result->error)) {
    $msg = '';

    // Parse the error to figure out what happened.
    if (preg_match("/<b>message<\\/b> <u>(.+?)<\\/u>/", $this->result->data, $matches)) {
      $msg = $matches[1];
    }
    drupal_set_message(t("Error connecting to AddThis.com service.<br/>\n!url<br/>\n@code @error %msg", array(
      '!url' => url(self::data_scheme . $url, array(
        'query' => $params,
      )),
      '@code' => $this->result->code,
      '@error' => $this->result->error,
      '%msg' => $msg,
    )), 'error');
    return FALSE;
  }
  return $this->result->data;
}