2009.02.11 19:17

AlexaSiteThumbnail Web Services

<?php
class alexa extends webservices {
public function thumbnail() {
$query = $this->parameters['url']; // full url
$domain = 'http://' . parse_url($query,PHP_URL_HOST) . '/'; // just the main domain page
$urls = array($query,$domain);

$access_key_id = $this->setting_variable('apikey.amazon.access_key_id');
$private_key = $this->setting_variable('apikey.amazon.secret_access_key');

$timestamp = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$timestamp_enc = urlencode($timestamp);
$signature_enc = urlencode ( $this->calculate_RFC2104HMAC("AlexaSiteThumbnail" . "Thumbnail" . $timestamp, $private_key));
$size = "Small";

$urls_param = "";
for ($i = 0; $i < count($urls); $i++) $urls_param .= "&Thumbnail." . ($i+1) . ".Url=" . urlencode($urls[$i]);

$request_url = "http://ast.amazonaws.com/xino/?"
. "Service=". "AlexaSiteThumbnail"
. "&Action=". "Thumbnail"
. "&AWSAccessKeyId=". $access_key_id
. "&Timestamp=" . $timestamp_enc
. "&Signature=" . $signature_enc
. "&Shared.Size=" . $size
. $urls_param;

$result = $this->get($request_url);

$response_doc = new DOMDocument();
$response_doc->loadXML($result);
$responses = $response_doc->getElementsByTagName("Response");

$results = array();

// full query
$thumbnail = $responses->item(0)->getElementsByTagName("Thumbnail")->item(0);
$results['full'] = array(
'thumbnail' => $thumbnail->firstChild->nodeValue
, 'found' => ($thumbnail->getAttribute("Exists") == "true") ? true : false
);

// domain query
$thumbnail = $responses->item(1)->getElementsByTagName("Thumbnail")->item(0);
$results['domain'] = array(
'thumbnail' => $thumbnail->firstChild->nodeValue
, 'found' => ($thumbnail->getAttribute("Exists") == "true") ? true : false
);

return $results;
}

private 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))))
);
}

}
?>

Trackback 0 Comment 0