Saturday, September 18, 2010

PHP Script to get Alexa rank using Alexa API

Here is an example PHP code that uses Alexa API to get Alexa rank. Note that for Alexa API, you need to purchase keys, as it's not free. Though it's not free cost is very low.

define("ACCESS_KEY_ID", "put key here");
define("SECRET_ACCESS_KEY", "put key here");
define("SERVICE_ENDPOINT", "http://awis.amazonaws.com?");

define("ACTION", "UrlInfo");
define("RESPONSE_GROUP", "Rank, LinksInCount");

$results = array();

function get_alexa_rank($site_url)
{
global $results;
$results = array();

$awis_url = generate_url($site_url);

$result = make_http_request($awis_url);

// Parse XML and display results

$current_tag = "";

$xml_parser = xml_parser_create("");
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($xml_parser, "start_tag", "end_tag");
xml_set_character_data_handler($xml_parser, "contents");
xml_parse($xml_parser, $result, true);
xml_parser_free($xml_parser);

return $results['rank']."|".$results['linksincount'];
}

function contents($parser, $data) {
global $current_tag, $results;
switch ($current_tag) {
case "aws:LinksInCount":
$results['linksincount'] .= $data;
break;
case "aws:Rank":
$results['rank'] .= $data;
break;
}
}

function start_tag($parser, $name) {
global $current_tag, $results;
$current_tag = $name;
}

function end_tag() {
global $current_tag;
$current_tag = '';

}

// Returns the AWS url to get AWIS information for the given site

function generate_url($site_url) {
$timestamp = generate_timestamp();
$site_enc = urlencode($site_url);
$timestamp_enc = urlencode($timestamp);
$signature_enc = urlencode(calculate_RFC2104HMAC(ACTION . $timestamp, SECRET_ACCESS_KEY));

return SERVICE_ENDPOINT
. "AWSAccessKeyId=".ACCESS_KEY_ID
. "&Action=".ACTION
. "&ResponseGroup=".RESPONSE_GROUP
. "&Timestamp=$timestamp_enc"
. "&Signature=$signature_enc"
. "&Url=$site_enc";

}


// Calculate signature using HMAC: http://www.faqs.org/rfcs/rfc2104.html

function calculate_RFC2104HMAC ($data, $key) {
return base64_encode (
pack("H*", sha1((str_pad($key, 64, chr(0x00))
^(str_repeat(chr(0x5c), 64))) .
pack("H*", sha1((str_pad($key, 64, chr(0x00))
^(str_repeat(chr(0x36), 64))) . $data))))
);
}

// Timestamp format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

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

// Make an http request to the specified URL and return the result

function make_http_request($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

No comments: