[Drupal] How to configure Alexa API to work with your Drupal 6 site - Part 1

| | 6 min read

What is Alexa API?

The Alexa API Module provides us comprehensive information regarding a website, such as page rank, count of site links, speed, categories etc. by passing the corresponding site URL. In order to achieve this, first of all, we have to register in the site http://aws.amazon.com/awis/
Once you register, you will be provided with Alexa API Access Key Id and Alexa API Secret Access Key. Since it is a paid service and based on request, passed payments are charged. Hence, remember to set an Alexa API Cache Expiry days say 90 days minimum.

You can download Sample Codes from here.

Now, you might ask the question as to why we need to create a custom module for this, if we already have the php script from the sample codes. We need to create the custom module for the following reasons:

  1. In the sample code, we have to enter Alexa API Access Key Id and Alexa API Secret Access Key in the code itself, which is not a good practice. If we have to change the key, we need to re-edit the code.
  2. Each time a user requests a data, the data has to be retrieved from the main site, which is quite expensive.
  3. Finally, we need to customize the display accordingly

Let us begin with this module. First of all, create a folder for your custom module and name it as alexaapi. Create an info file named alexaapi.info and an install file named alexaapi.install to store site details in your Data Base.

Now, let us go back to our sample code. We have re-edited this sample code to get all the details in one pass, to make it more user-friendly. See the code below.

<?php

/** @file

 * Makes a request to AWIS for site info.

 * File name "aws.class.php"

 */

include_once("xml.class.php");

class aws {

  function aws() {

    $this->serviceHost        = 'awis.amazonaws.com';

    $this->numReturn          = numReturn;

    $this->sigVersion         = sigVersion;

    $this->hashAlgorithm      = hashAlgorithm;

    $this->accessKeyId        = accessKeyId;

    $this->secretAccessKey    = secretAccessKey;

  }

  /**

   * Get site info from AWIS.

   */

  public function getUrlInfo($pmSite, $pmParam) {

    $this->site = $pmSite;

    $this->actionName = 'UrlInfo';

    $this->responseGroupName = $pmParam['responseGroupName'];

    $params = array(

        'Action'            => $this->actionName,

        'ResponseGroup'     => $this->responseGroupName,

        'AWSAccessKeyId'    => $this->accessKeyId,

        'Timestamp'         => $this->getTimestamp(),

        'SignatureVersion'  => $this->sigVersion,

        'SignatureMethod'   => $this->hashAlgorithm,

        'Url'               => $this->site

    );

    ksort($params);

    $keyvalue = array();

    foreach ($params as $k => $v) {

        $keyvalue[] = $k . '=' . rawurlencode($v);

    }

    $queryParams = implode('&', $keyvalue);

    $sig = $this->generateSignature($queryParams);

    $url = 'http://' . $this->serviceHost . '/?' . $queryParams . '&Signature=' . $sig;

    return $ret = self::makeRequest($url);

  }

  public function getTraficHistory($pmSite, $pmParam) {

    $this->site = $pmSite;

    $this->actionName = 'TrafficHistory';

    $this->responseGroupName = $pmParam['responseGroupName'];

    $this->responseGroupName = $this->responseGroupName;

    $this->startDate         = $pmParam['startDate'];

    $params = array(

        'Action'            => $this->actionName,

        'ResponseGroup'     => $this->responseGroupName,

        'AWSAccessKeyId'    => $this->accessKeyId,

        'Timestamp'         => $this->getTimestamp(),

        'Range'             => $this->numReturn,

        'SignatureVersion'  => $this->sigVersion,

        'SignatureMethod'   => $this->hashAlgorithm,

        'Start'             => $this->startDate,

        'Url'               => $this->site

    );

    ksort($params);

    $keyvalue = array();

    foreach ($params as $k => $v) {

      $keyvalue[] = $k . '=' . rawurlencode($v);

    }

    $queryParams = implode('&', $keyvalue);

    $sig = $this->generateSignature($queryParams);

    $url = 'http://' . $this->serviceHost . '/?' . $queryParams . '&Signature=' . $sig;

    return $ret = self::makeRequest($url);

  }

  /**

   * Builds current ISO8601 timestamp.

   */

  protected static function getTimestamp() {

    return gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());

  }

  /**

   * Makes request to AWIS

   * @param String $url   URL to make request to

   * @return String       Result of request

   */

  protected static function makeRequest($url) {

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_TIMEOUT, 4);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($ch);

    curl_close($ch);

    $result = str_replace('aws:', '', $result);

    $result = str_replace(':aws', '', $result);

    return $aXml = xml_to_array($result);

  }

  /**

   * Generates an HMAC signature per RFC 2104.

   *

   * @param String $url       URL to use in createing signature

   */

  protected function generateSignature($url) {

    $sign = "GET\n" . drupal_strtolower($this->serviceHost) . "\n/\n". $url;

    //echo "String to sign: \n" . $sign . "\n";

    $sig = base64_encode(hash_hmac('sha256', $sign, $this->secretAccessKey, TRUE));

    //echo "\nSignature: " . $sig ."\n";

    return rawurlencode($sig);

  }

}

In the above code we include xml.class.php file. In this php script we use the function xml_to_array() which will convert the given XML text to an array in the XML structure. Arguments passed are:

  • $contents - The XML text
  • $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
  • $priority - Can be 'tag' or 'attribute'. This will change the resulting array structure. For 'tag', the tags are given more importance.

This function returns the parsed XML in an array form.

<?php

/** @file

 * File name "xml.class.php"

 * 

 */

function xml_to_array($contents, $get_attributes=1, $priority = 'values') {

    if (!$contents) return array();

    if (!function_exists('xml_parser_create')) {

        //print "'xml_parser_create()' function not found!";

        return array();

    }

    //Get the XML parser of PHP - PHP must have this module for the parser to work

    $parser = xml_parser_create('');

    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");

    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);

    xml_parse_into_struct($parser, trim($contents), $xml_values);

    xml_parser_free($parser);

    if (!$xml_values) return;

    //Initializations

    $xml_array = array();

    $parents = array();

    $opened_tags = array();

    $arr = array();

    $current = &$xml_array; //Refference

    //Go through the tags.

    $repeated_tag_index = array();//Multiple tags with same name will be turned into an array

    foreach ($xml_values as $data) {

        unset($attributes, $value);//Remove existing values, or there will be trouble

        //This command will extract these variables into the foreach scope

        // tag(string), type(string), level(int), attributes(array).

        extract($data);//We could use the array by itself, but this cooler.

        $result = array();

        $attributes_data = array();

        if (isset($value)) {

            if ($priority == 'tag') $result = $value;

            else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode

        }

        //Set the attributes too.

        if (isset($attributes) and $get_attributes) {

            foreach ($attributes as $attr => $val) {

                if ($priority == 'tag') $attributes_data[$attr] = $val;

                else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'

            }

        }

        //See tag status and do the needed.

        if ($type == "open") {//The starting of the tag '<tag>'

            $parent[$level-1] = &$current;

            if (!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag

                $current[$tag] = $result;

                if ($attributes_data) $current[$tag . '_attr'] = $attributes_data;

                $repeated_tag_index[$tag . '_' . $level] = 1;

                $current = &$current[$tag];

            }

            else { //There was another element with the same tag name

                if (isset($current[$tag][0])) {//If there is a 0th element it is already an array

                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;

                    $repeated_tag_index[$tag . '_' . $level]++;

                }

                else {//This section will make the value an array if multiple tags with the same name appear together

                    $current[$tag] = array($current[$tag], $result);//This will combine the existing item and the new item together to make an array

                    $repeated_tag_index[$tag . '_' . $level] = 2;

                    if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well

                        $current[$tag]['0_attr'] = $current[$tag . '_attr'];

                        unset($current[$tag . '_attr']);

                    }

                }

                $last_item_index = $repeated_tag_index[$tag . '_' . $level]-1;

                $current = &$current[$tag][$last_item_index];

            }

        }

        elseif ($type == "complete") { //Tags that ends in 1 line '<tag />'

            //See if the key is already taken.

            if (!isset($current[$tag])) { //New Key

                $current[$tag] = $result;

                $repeated_tag_index[$tag . '_' . $level] = 1;

                if ($priority == 'tag' and $attributes_data) $current[$tag . '_attr'] = $attributes_data;

            }

            else { //If taken, put all things inside a list(array)

                if (isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...

                    // ...push the new element into that array.

                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;

                    if ($priority == 'tag' and $get_attributes and $attributes_data) {

                        $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;

                    }

                    $repeated_tag_index[$tag . '_' . $level]++;

                }

                else { //If it is not an array...

                    $current[$tag] = array($current[$tag], $result); //...Make it an array using using the existing value and the new value

                    $repeated_tag_index[$tag . '_' . $level] = 1;

                    if ($priority == 'tag' and $get_attributes) {

                        if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well

                            $current[$tag]['0_attr'] = $current[$tag . '_attr'];

                            unset($current[$tag . '_attr']);

                        }

                        if ($attributes_data) {

                            $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;

                        }

                    }

                    $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken

                }

            }

        }

        elseif ($type == 'close') { //End of tag '</tag>'

            $current = &$parent[$level-1];

        }

    }

    return ($xml_array);

}

Once you have created these two files, add them into your custom module folder.

Now comes the creation of alexaapi.module, which will be continued in the second part "How to implement Alexa API Module in Drupal 6 Part 2"