· 6 years ago · Mar 31, 2020, 06:18 PM
1<?php
2/**
3 * Makes a request to AWIS for site info.
4 */
5class UrlInfo {
6
7 protected static $ActionName = 'UrlInfo';
8 protected static $ResponseGroupName = 'Rank,LinksInCount';
9 protected static $ServiceHost = 'awis.amazonaws.com';
10 protected static $ServiceEndpoint = 'awis.us-west-1.amazonaws.com';
11 protected static $NumReturn = 10;
12 protected static $StartNum = 1;
13 protected static $SigVersion = '2';
14 protected static $HashAlgorithm = 'HmacSHA256';
15 protected static $ServiceURI = "/api";
16 protected static $ServiceRegion = "us-west-1";
17 protected static $ServiceName = "awis";
18
19
20 public function UrlInfo($accessKeyId, $secretAccessKey, $site) {
21 $this->accessKeyId = $accessKeyId;
22 $this->secretAccessKey = $secretAccessKey;
23 $this->site = $site;
24 $now = time();
25 $this->amzDate = gmdate("Ymd\THis\Z", $now);
26 $this->dateStamp = gmdate("Ymd", $now);
27
28 }
29
30 /**
31 * Get site info from AWIS.
32 */
33 public function getUrlInfo() {
34 $canonicalQuery = $this->buildQueryParams();
35 $canonicalHeaders = $this->buildHeaders(true);
36 $signedHeaders = $this->buildHeaders(false);
37 $payloadHash = hash('sha256', "");
38 $canonicalRequest = "GET" . "\n" . self::$ServiceURI . "\n" . $canonicalQuery . "\n" . $canonicalHeaders . "\n" . $signedHeaders . "\n" . $payloadHash;
39 $algorithm = "AWS4-HMAC-SHA256";
40 $credentialScope = $this->dateStamp . "/" . self::$ServiceRegion . "/" . self::$ServiceName . "/" . "aws4_request";
41 $stringToSign = $algorithm . "\n" . $this->amzDate . "\n" . $credentialScope . "\n" . hash('sha256', $canonicalRequest);
42 $signingKey = $this->getSignatureKey();
43 $signature = hash_hmac('sha256', $stringToSign, $signingKey);
44 $authorizationHeader = $algorithm . ' ' . 'Credential=' . $this->accessKeyId . '/' . $credentialScope . ', ' . 'SignedHeaders=' . $signedHeaders . ', ' . 'Signature=' . $signature;
45
46 $url = 'https://' . self::$ServiceHost . self::$ServiceURI . '?' . $canonicalQuery;
47 $ret = self::makeRequest($url, $authorizationHeader);
48 //echo "\nResults for " . $this->site .":\n\n";
49 //echo $ret;
50 self::parseResponse($ret);
51 }
52
53 protected function sign($key, $msg) {
54 return hash_hmac('sha256', $msg, $key, true);
55 }
56
57 protected function getSignatureKey() {
58 $kSecret = 'AWS4' . $this->secretAccessKey;
59 $kDate = $this->sign($kSecret, $this->dateStamp);
60 $kRegion = $this->sign($kDate, self::$ServiceRegion);
61 $kService = $this->sign($kRegion, self::$ServiceName);
62 $kSigning = $this->sign($kService, 'aws4_request');
63 return $kSigning;
64 }
65
66 /**
67 * Builds headers for the request to AWIS.
68 * @return String headers for the request
69 */
70 protected function buildHeaders($list) {
71 $params = array(
72 'host' => self::$ServiceEndpoint,
73 'x-amz-date' => $this->amzDate
74 );
75 ksort($params);
76 $keyvalue = array();
77 foreach($params as $k => $v) {
78 if ($list)
79 $keyvalue[] = $k . ':' . $v;
80 else {
81 $keyvalue[] = $k;
82 }
83 }
84 return ($list) ? implode("\n",$keyvalue) . "\n" : implode(';',$keyvalue) ;
85 }
86
87 /**
88 * Builds query parameters for the request to AWIS.
89 * Parameter names will be in alphabetical order and
90 * parameter values will be urlencoded per RFC 3986.
91 * @return String query parameters for the request
92 */
93 protected function buildQueryParams() {
94 $params = array(
95 'Action' => self::$ActionName,
96 'Count' => self::$NumReturn,
97 'ResponseGroup' => self::$ResponseGroupName,
98 'Start' => self::$StartNum,
99 'Url' => $this->site
100 );
101 ksort($params);
102 $keyvalue = array();
103 foreach($params as $k => $v) {
104 $keyvalue[] = $k . '=' . rawurlencode($v);
105 }
106 return implode('&',$keyvalue);
107 }
108
109 /**
110 * Makes request to AWIS
111 * @param String $url URL to make request to
112 * @param String authorizationHeader Authorization string
113 * @return String Result of request
114 */
115 protected function makeRequest($url, $authorizationHeader) {
116 echo "\nMaking request to:\n$url\n";
117 $ch = curl_init($url);
118 curl_setopt($ch, CURLOPT_TIMEOUT, 4);
119 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
120 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
121 'Accept: application/xml',
122 'Content-Type: application/xml',
123 'X-Amz-Date: ' . $this->amzDate,
124 'Authorization: ' . $authorizationHeader
125 ));
126 $result = curl_exec($ch);
127 curl_close($ch);
128 return $result;
129 }
130
131 /**
132 * Parses XML response from AWIS and displays selected data
133 * @param String $response xml response from AWIS
134 */
135 public static function parseResponse($response) {
136 $xml = new SimpleXMLElement($response,LIBXML_ERR_ERROR,false,'http://awis.amazonaws.com/doc/2005-07-11');
137 if($xml->count() && $xml->Response->UrlInfoResult->Alexa->count()) {
138 $info = $xml->Response->UrlInfoResult->Alexa;
139 $nice_array = array(
140 'Links In Count' => $info->ContentData->LinksInCount,
141 'Rank' => $info->TrafficData->Rank
142 );
143 }
144
145 $apiInfo = array();
146 foreach($nice_array as $k => $v) {
147 echo $k . ': ' . $v ."\n <br>";
148 array_push($apiInfo, $v);
149 }
150 }
151
152}
153
154
155
156$urlInfo = new UrlInfo("AKIAX66LNZH4UZIKDSQG", "jQ83ThwypYmgULsHwCF32eSauAXuvI85dJ0tiPeg", "amazon.com");
157
158$urlInfo->getUrlInfo();
159print_r($apiInfo);
160?>