<?php

// Make a request to the Alexa Top Sites service to get lists of top sites

define("ACCESS_KEY", "0T5FGC75J7R00E9QPGG2");

define("SECRET_ACCESS_KEY", "***********");

define("SERVICE_ENDPOINT", "http://ats.amazonaws.com/?");

define("ACTION", "TopSites");

define("RESPONSE_GROUP", "country");

$start = 1;
$count = 100;  // Max value is 100
$country = "UK";


echo("For country: " . $country."\n\n");

$ats_url = compose_url($country, $start, $count);


echo ("Request: \n". $ats_url."\n\n");


// Make request

$result = make_http_request($ats_url);


// Display resulting XML

//echo("Response:\n");
//echo($result);


// Parse XML and display sites list

$sites_list = array();
$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);


// Print out the results

$end = $start + $count -1;

echo "Sites: $start to $end from $country list:\n";


foreach ($sites_list as $site) {
        echo "$site\n";

}

function contents($parser, $data){
    global $current_tag, $sites_list;
    switch ($current_tag) {
        case "aws:DataUrl":
                array_push( $sites_list, $data);
    }
}

function start_tag($parser, $name) {
    global $current_tag;

    $current_tag = $name;

}

function end_tag($parser, $name) {
    global $current_tag;

    $current_tag = "";

}

// Returns the AWS url to get the top sites list for the given country starting at start,
//  and returning 'count' results

function compose_url($country, $start, $count) {

        $timestamp =  generate_timestamp();

        $timestamp_enc = urlencode($timestamp);

        $signature_enc = urlencode (
            calculate_RFC2104HMAC
                    (ACTION . $timestamp, SECRET_ACCESS_KEY)
            );


        return  SERVICE_ENDPOINT
                        . "Action=".ACTION
                        . "&AWSAccessKeyId=".ACCESS_KEY
                        . "&ResponseGroup=".RESPONSE_GROUP
                        . "&Timestamp=$timestamp_enc"
                        . "&Signature=$signature_enc"
                        . "&Start=$start"
                        . "&Count=$count"
                        . "&CountryCode=$country";


}


// 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



$data = file_get_contents($url);
if ($data !== false) {
return $result;// success code here
}
else
  // failure code here

?>