You are here

mostpopular_addthis.classes.inc in Drupal Most Popular 7

Same filename and directory in other branches
  1. 6 modules/mostpopular_addthis/mostpopular_addthis.classes.inc

Provides a connector to the AddThis.com API.

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

File

modules/mostpopular_addthis/mostpopular_addthis.classes.inc
View source
<?php

// $Id$



/**
 * @file
 * Provides a connector to the AddThis.com API.
 *
 * See http://www.addthis.com/help/analytics-api
 */
class MostPopularAddThis {
  const data_scheme = 'https://';
  const data_url = 'api.addthis.com/analytics/1.0/pub/shares';
  protected $username;
  protected $password;
  protected $pubid;
  protected $domain;
  protected $result;

  /**
   * Constructs a new AddThis class.
   */
  public function MostPopularAddThis($username, $password, $pubid, $domain) {
    $this->username = $username;
    $this->password = $password;
    $this->pubid = $pubid;
    $this->domain = $domain;
  }

  /**
   * Makes a generic call to the AddThis.com server.
   *
   * See http://www.addthis.com/help/analytics-api
   *
   * @param string $dimension
   *   The dimension we're interested in.  It must be null or one of:
   *   - url
   *   - continent
   *   - country
   *   - domain
   *   - service
   * @param string $format
   *   The format to return results, either 'csv' or 'json'.
   * @param string $params
   *   Additional parameters to send.  The following are allowed:
   *   - period: 'day', 'week' or 'month'
   *   - domain: any URL domain
   *   - service: 'email'
   */
  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;
  }

  /**
   * Makes a call to the AddThis.com server and parses the JSON response.
   *
   * @param string $dimension
   *   The dimension we're interested in.  It must be null or one of:
   *   - url
   *   - continent
   *   - country
   *   - domain
   *   - service
   * @param string $params
   *   Additional parameters to send.  The following are allowed:
   *   - period: 'day', 'week' or 'month'
   *   - domain: any URL domain
   *   - service: 'email'
   *
   * @return array
   *   An array of associative arrays, each containing:
   *     - shares: The number of times the page was shared.
   *     - url: The URL of the page.
   */
  public function fetchJson($dimension = '', $params = array()) {
    $data = $this
      ->fetch($dimension, 'json', $params);
    if ($data !== FALSE) {
      return drupal_json_decode($data);
    }
    return FALSE;
  }

}

Classes

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