Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Makes a request to AWIS for site info.
- */
- class UrlInfo {
- protected static $ActionName = 'UrlInfo';
- protected static $ResponseGroupName = 'Rank,RankByCountry';
- protected static $ServiceHost = 'awis.amazonaws.com';
- protected static $ServiceEndpoint = 'awis.us-west-1.amazonaws.com';
- protected static $NumReturn = 10;
- protected static $StartNum = 1;
- protected static $SigVersion = '2';
- protected static $HashAlgorithm = 'HmacSHA256';
- protected static $ServiceURI = "/api";
- protected static $ServiceRegion = "us-west-1";
- protected static $ServiceName = "awis";
- public function UrlInfo($accessKeyId, $secretAccessKey, $site) {
- $this->accessKeyId = $accessKeyId;
- $this->secretAccessKey = $secretAccessKey;
- $this->site = $site;
- $now = time();
- $this->amzDate = gmdate("Ymd\THis\Z", $now);
- $this->dateStamp = gmdate("Ymd", $now);
- }
- /**
- * Get site info from AWIS.
- */
- public function getUrlInfo() {
- $canonicalQuery = $this->buildQueryParams();
- $canonicalHeaders = $this->buildHeaders(true);
- $signedHeaders = $this->buildHeaders(false);
- $payloadHash = hash('sha256', "");
- $canonicalRequest = "GET" . "\n" . self::$ServiceURI . "\n" . $canonicalQuery . "\n" . $canonicalHeaders . "\n" . $signedHeaders . "\n" . $payloadHash;
- $algorithm = "AWS4-HMAC-SHA256";
- $credentialScope = $this->dateStamp . "/" . self::$ServiceRegion . "/" . self::$ServiceName . "/" . "aws4_request";
- $stringToSign = $algorithm . "\n" . $this->amzDate . "\n" . $credentialScope . "\n" . hash('sha256', $canonicalRequest);
- $signingKey = $this->getSignatureKey();
- $signature = hash_hmac('sha256', $stringToSign, $signingKey);
- $authorizationHeader = $algorithm . ' ' . 'Credential=' . $this->accessKeyId . '/' . $credentialScope . ', ' . 'SignedHeaders=' . $signedHeaders . ', ' . 'Signature=' . $signature;
- $url = 'https://' . self::$ServiceHost . self::$ServiceURI . '?' . $canonicalQuery;
- $ret = self::makeRequest($url, $authorizationHeader);
- //echo "\nResults for " . $this->site .":\n\n";
- //echo $ret;
- return self::parseResponse($ret);
- }
- protected function sign($key, $msg) {
- return hash_hmac('sha256', $msg, $key, true);
- }
- protected function getSignatureKey() {
- $kSecret = 'AWS4' . $this->secretAccessKey;
- $kDate = $this->sign($kSecret, $this->dateStamp);
- $kRegion = $this->sign($kDate, self::$ServiceRegion);
- $kService = $this->sign($kRegion, self::$ServiceName);
- $kSigning = $this->sign($kService, 'aws4_request');
- return $kSigning;
- }
- /**
- * Builds headers for the request to AWIS.
- * @return String headers for the request
- */
- protected function buildHeaders($list) {
- $params = array(
- 'host' => self::$ServiceEndpoint,
- 'x-amz-date' => $this->amzDate
- );
- ksort($params);
- $keyvalue = array();
- foreach($params as $k => $v) {
- if ($list)
- $keyvalue[$k] = $k . ':' . $v;
- else {
- $keyvalue[] = $k;
- }
- }
- return ($list) ? implode("\n",$keyvalue) . "\n" : implode(';',$keyvalue) ;
- }
- /**
- * 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,
- 'Count' => self::$NumReturn,
- 'ResponseGroup' => self::$ResponseGroupName,
- 'Start' => self::$StartNum,
- '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
- * @param String authorizationHeader Authorization string
- * @return String Result of request
- */
- protected function makeRequest($url, $authorizationHeader) {
- //echo "\nMaking request to:\n$url\n";
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_TIMEOUT, 4);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Accept: application/xml',
- 'Content-Type: application/xml',
- 'X-Amz-Date: ' . $this->amzDate,
- 'Authorization: ' . $authorizationHeader
- ));
- $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,LIBXML_ERR_ERROR,false,'http://awis.amazonaws.com/doc/2005-07-11');
- //return $response;
- if($xml->count() && $xml->Response->UrlInfoResult->Alexa->count())
- {
- $info = $xml->Response->UrlInfoResult->Alexa;
- $nice_array = array(
- //'Links In Count' => $info->ContentData->LinksInCount,
- 'Rank' => $info->TrafficData->Rank,
- 'RankByCountry' => (object) array()
- );
- foreach ($info->TrafficData->RankByCountry->Country as $key => $value)
- {
- $attr = (array) $value->attributes();
- $nice_array['RankByCountry']->Country[$attr['@attributes']['Code']] = $value;
- }
- }
- //$apiInfo = array();
- //foreach($nice_array as $k => $v) {
- //echo $k . ': ' . $v ."\n <br>";
- //$apiInfo[$k] = $v;
- //array_push($apiInfo, $v);
- //}
- //return $apiInfo;
- return $nice_array;
- }
- }
- $urlInfo = new UrlInfo($accessKeyId, $secretAccessKey, $site);
- $getSiteInfo = $urlInfo->getUrlInfo();
- //print_r($getSiteInfo);
- function xml2array ( $getSiteInfo, $out = array () )
- {
- foreach ( (array) $getSiteInfo as $index => $node )
- $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
- return $out;
- }
- $fstLevel = xml2array($getSiteInfo['RankByCountry']);
- $countryRank = xml2array($fstLevel['Country']['US']);
- echo "Rank : ". $getSiteInfo['Rank']."<br>";
- echo "US Rank : ". $countryRank['Rank'];
- ?>
Advertisement
Add Comment
Please, Sign In to add comment