Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@michaelwowro
Created November 22, 2012 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelwowro/4130431 to your computer and use it in GitHub Desktop.
Save michaelwowro/4130431 to your computer and use it in GitHub Desktop.
PHP-script to call the AlexaWebInformationService over the webserver
<?php
/**
* This script is just a template - you have to adapt it to your needs.
* WHY YOU MIGHT NEED IT
* That script (http://aws.amazon.com/code/AWIS/402) provided by Alexa is optimized for running on a console. This script here is optimized for running on a webserver.
* Also the Alexa-script isn't running on older PHP (because it uses based on newer version of SimpleXMLElement::__construct with more than three parameters). This script here is running with at least PHP 5.1.6 (24. August 2006).
*
* INSTRUCTIONS
* 1.) You have to get an account at Amazon Web Services (for that you need a valid credit card).
* 2.) You have to apply for Alexa Web Information Service additionally.
* 3.) Here you find your accessKeyId and secretAccessKey which you have to fill in the appropriate variables in the beginning of the script.
* 4.) Assign your webpage in variable $site.
* 5.) Load that script then on your webserver and finally open in your browser.
*
* SUPPORT
* You can find Alexa-AWS specific help here: https://forums.aws.amazon.com/forum.jspa?forumID=14
**/
$accessKeyId = "your_accessKeyId";
$secretAccessKey = "your_secretAccessKey";
$site = "http://seleniumhq.org";
class UrlInfo {
protected static $ActionName = 'UrlInfo';
protected static $ResponseGroupName = 'Rank,ContactInfo,LinksInCount';
protected static $ServiceHost = 'awis.amazonaws.com';
protected static $NumReturn = 10;
protected static $StartNum = 1;
protected static $SigVersion = '2';
protected static $HashAlgorithm = 'HmacSHA256';
public function UrlInfo($accessKeyId, $secretAccessKey, $site) {
$this->accessKeyId = $accessKeyId;
$this->secretAccessKey = $secretAccessKey;
$this->site = $site;
}
/**
* Get site info from AWIS.
*/
public function getUrlInfo() {
$queryParams = $this->buildQueryParams();
$sig = $this->generateSignature($queryParams);
$url = 'http://' . self::$ServiceHost . '/?' . $queryParams .
'&Signature=' . $sig;
$ret = self::makeRequest($url);
echo "<b>Results for " . $this->site .":</b><br>";
self::parseResponse($ret);
}
/**
* Builds current ISO8601 timestamp.
*/
protected static function getTimestamp() {
return gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
}
/**
* Builds query parameters for the request to AWIS.
* Parameter names will be in alphabetical order and
* parameter values will be urlencoded per RFC 3986.
* @return String query parameters for the request
*/
protected function buildQueryParams() {
$params = array(
'Action' => self::$ActionName,
'ResponseGroup' => self::$ResponseGroupName,
'AWSAccessKeyId' => $this->accessKeyId,
'Timestamp' => self::getTimestamp(),
'Count' => self::$NumReturn,
'Start' => self::$StartNum,
'SignatureVersion' => self::$SigVersion,
'SignatureMethod' => self::$HashAlgorithm,
'Url' => $this->site
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
$keyvalue[] = $k . '=' . rawurlencode($v);
}
return implode('&',$keyvalue);
}
/**
* Makes request to AWIS
* @param String $url URL to make request to
* @return String Result of request
*/
protected static function makeRequest($url) {
echo "<br><b>Making request to</b><i> (you can copy&paste this url to your browser's address bar to see if it works. You have to do this within few minutes.):</i><br>".$url."<br><br>";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* Parses XML response from AWIS and displays selected data
* @param String $response xml response from AWIS
*/
public static function parseResponse($response) {
$xml = new SimpleXMLElement($response);
//you have to show the namespace to SimpleXMLElement
$clean_xml = $xml->children('http://awis.amazonaws.com/doc/2005-07-11');
$info = $clean_xml->Response->UrlInfoResult->Alexa;
$nice_array = array(
'Rank' => $info->TrafficData->Rank,
'Links In Count' => $info->ContentData->LinksInCount,
'Phone Number' => $info->ContactInfo->PhoneNumbers->PhoneNumber,
'Owner Name' => $info->ContactInfo->OwnerName,
'Email' => $info->ContactInfo->Email,
'Street' => $info->ContactInfo->PhysicalAddress->Streets->Street,
'City' => $info->ContactInfo->PhysicalAddress->City,
'State' => $info->ContactInfo->PhysicalAddress->State,
'Postal Code' => $info->ContactInfo->PhysicalAddress->PostalCode,
'Country' => $info->ContactInfo->PhysicalAddress->Country
);
foreach($nice_array as $k => $v) {
echo $k . ': ' . $v ."<br>";
}
}
/**
* Generates an HMAC signature per RFC 2104.
*
* @param String $url URL to use in createing signature
*/
protected function generateSignature($url) {
$sign = "GET\n" . strtolower(self::$ServiceHost) . "\n/\n". $url;
echo "<b>String to sign:</b> <br>" . $sign . " <br>";
$sig = base64_encode(hash_hmac('sha256', $sign, $this->secretAccessKey, true));
echo "<br><b>Signature </b><br>" . removeFromEnd($sig, '=') ."<br>";
return rawurlencode($sig);
}
}
// to remove the equal sign of Signature before printing to the screen
function removeFromEnd($string, $stringToRemove) {
$stringToRemoveLen = strlen($stringToRemove);
$stringLen = strlen($string);
$pos = $stringLen - $stringToRemoveLen;
$out = substr($string, 0, $pos);
return $out;
}
$urlInfo = new UrlInfo($accessKeyId, $secretAccessKey, $site);
$urlInfo->getUrlInfo();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment