Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. <?php
  2.  
  3. /* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
  4. /* Licensed under the Apache License, Version 2.0. */
  5.  
  6. // Put your Secret Key in place of **********
  7. $serviceName="ProductAdvertisingAPI";
  8. $region="eu-west-1";
  9. $accessKey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  10. $secretKey="**********";
  11. $payload="{"
  12. ." \"ItemIds\": ["
  13. ." \"ASINXXXX\""
  14. ." ],"
  15. ." \"Resources\": ["
  16. ." \"Images.Primary.Small\","
  17. ." \"Images.Primary.Medium\","
  18. ." \"Images.Primary.Large\","
  19. ." \"Images.Variants.Small\","
  20. ." \"Images.Variants.Medium\","
  21. ." \"Images.Variants.Large\","
  22. ." \"ItemInfo.ByLineInfo\","
  23. ." \"ItemInfo.ContentInfo\","
  24. ." \"ItemInfo.ContentRating\","
  25. ." \"ItemInfo.Classifications\","
  26. ." \"ItemInfo.ExternalIds\","
  27. ." \"ItemInfo.Features\","
  28. ." \"ItemInfo.ManufactureInfo\","
  29. ." \"ItemInfo.ProductInfo\","
  30. ." \"ItemInfo.TechnicalInfo\","
  31. ." \"ItemInfo.Title\","
  32. ." \"ItemInfo.TradeInInfo\","
  33. ." \"Offers.Listings.DeliveryInfo.IsFreeShippingEligible\","
  34. ." \"Offers.Listings.DeliveryInfo.IsPrimeEligible\","
  35. ." \"Offers.Listings.MerchantInfo\","
  36. ." \"Offers.Listings.Price\""
  37. ." ],"
  38. ." \"PartnerTag\": \"XXXXXXXXXXXXXXXXXXXXXXX-21\","
  39. ." \"PartnerType\": \"Associates\","
  40. ." \"Marketplace\": \"www.amazon.it\""
  41. ."}";
  42. $host="webservices.amazon.it";
  43. $uriPath="/paapi5/getitems";
  44. $awsv4 = new AwsV4 ($accessKey, $secretKey);
  45. $awsv4->setRegionName($region);
  46. $awsv4->setServiceName($serviceName);
  47. $awsv4->setPath ($uriPath);
  48. $awsv4->setPayload ($payload);
  49. $awsv4->setRequestMethod ("POST");
  50. $awsv4->addHeader ('content-encoding', 'amz-1.0');
  51. $awsv4->addHeader ('content-type', 'application/json; charset=utf-8');
  52. $awsv4->addHeader ('host', $host);
  53. $awsv4->addHeader ('x-amz-target', 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems');
  54. $headers = $awsv4->getHeaders ();
  55. $headerString = "";
  56. foreach ( $headers as $key => $value ) {
  57. $headerString .= $key . ': ' . $value . "\r\n";
  58. }
  59. $params = array (
  60. 'http' => array (
  61. 'header' => $headerString,
  62. 'method' => 'POST',
  63. 'content' => $payload
  64. )
  65. );
  66. $stream = stream_context_create ( $params );
  67.  
  68. $fp = @fopen ( 'https://'.$host.$uriPath, 'rb', false, $stream );
  69.  
  70. if (! $fp) {
  71. throw new Exception ( "Exception Occured" );
  72. }
  73. $response = @stream_get_contents ( $fp );
  74. if ($response === false) {
  75. throw new Exception ( "Exception Occured" );
  76. }
  77. echo $response;
  78.  
  79. class AwsV4 {
  80.  
  81. private $accessKey = null;
  82. private $secretKey = null;
  83. private $path = null;
  84. private $regionName = null;
  85. private $serviceName = null;
  86. private $httpMethodName = null;
  87. private $queryParametes = array ();
  88. private $awsHeaders = array ();
  89. private $payload = "";
  90.  
  91. private $HMACAlgorithm = "AWS4-HMAC-SHA256";
  92. private $aws4Request = "aws4_request";
  93. private $strSignedHeader = null;
  94. private $xAmzDate = null;
  95. private $currentDate = null;
  96.  
  97. public function __construct($accessKey, $secretKey) {
  98. $this->accessKey = $accessKey;
  99. $this->secretKey = $secretKey;
  100. $this->xAmzDate = $this->getTimeStamp ();
  101. $this->currentDate = $this->getDate ();
  102. }
  103.  
  104. function setPath($path) {
  105. $this->path = $path;
  106. }
  107.  
  108. function setServiceName($serviceName) {
  109. $this->serviceName = $serviceName;
  110. }
  111.  
  112. function setRegionName($regionName) {
  113. $this->regionName = $regionName;
  114. }
  115.  
  116. function setPayload($payload) {
  117. $this->payload = $payload;
  118. }
  119.  
  120. function setRequestMethod($method) {
  121. $this->httpMethodName = $method;
  122. }
  123.  
  124. function addHeader($headerName, $headerValue) {
  125. $this->awsHeaders [$headerName] = $headerValue;
  126. }
  127.  
  128. private function prepareCanonicalRequest() {
  129. $canonicalURL = "";
  130. $canonicalURL .= $this->httpMethodName . "\n";
  131. $canonicalURL .= $this->path . "\n" . "\n";
  132. $signedHeaders = '';
  133. foreach ( $this->awsHeaders as $key => $value ) {
  134. $signedHeaders .= $key . ";";
  135. $canonicalURL .= $key . ":" . $value . "\n";
  136. }
  137. $canonicalURL .= "\n";
  138. $this->strSignedHeader = substr ( $signedHeaders, 0, - 1 );
  139. $canonicalURL .= $this->strSignedHeader . "\n";
  140. $canonicalURL .= $this->generateHex ( $this->payload );
  141. return $canonicalURL;
  142. }
  143.  
  144. private function prepareStringToSign($canonicalURL) {
  145. $stringToSign = '';
  146. $stringToSign .= $this->HMACAlgorithm . "\n";
  147. $stringToSign .= $this->xAmzDate . "\n";
  148. $stringToSign .= $this->currentDate . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "\n";
  149. $stringToSign .= $this->generateHex ( $canonicalURL );
  150. return $stringToSign;
  151. }
  152.  
  153. private function calculateSignature($stringToSign) {
  154. $signatureKey = $this->getSignatureKey ( $this->secretKey, $this->currentDate, $this->regionName, $this->serviceName );
  155. $signature = hash_hmac ( "sha256", $stringToSign, $signatureKey, true );
  156. $strHexSignature = strtolower ( bin2hex ( $signature ) );
  157. return $strHexSignature;
  158. }
  159.  
  160. public function getHeaders() {
  161. $this->awsHeaders ['x-amz-date'] = $this->xAmzDate;
  162. ksort ( $this->awsHeaders );
  163.  
  164. // Step 1: CREATE A CANONICAL REQUEST
  165. $canonicalURL = $this->prepareCanonicalRequest ();
  166.  
  167. // Step 2: CREATE THE STRING TO SIGN
  168. $stringToSign = $this->prepareStringToSign ( $canonicalURL );
  169.  
  170. // Step 3: CALCULATE THE SIGNATURE
  171. $signature = $this->calculateSignature ( $stringToSign );
  172.  
  173. // Step 4: CALCULATE AUTHORIZATION HEADER
  174. if ($signature) {
  175. $this->awsHeaders ['Authorization'] = $this->buildAuthorizationString ( $signature );
  176. return $this->awsHeaders;
  177. }
  178. }
  179.  
  180. private function buildAuthorizationString($strSignature) {
  181. return $this->HMACAlgorithm . " " . "Credential=" . $this->accessKey . "/" . $this->getDate () . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "," . "SignedHeaders=" . $this->strSignedHeader . "," . "Signature=" . $strSignature;
  182. }
  183.  
  184. private function generateHex($data) {
  185. return strtolower ( bin2hex ( hash ( "sha256", $data, true ) ) );
  186. }
  187.  
  188. private function getSignatureKey($key, $date, $regionName, $serviceName) {
  189. $kSecret = "AWS4" . $key;
  190. $kDate = hash_hmac ( "sha256", $date, $kSecret, true );
  191. $kRegion = hash_hmac ( "sha256", $regionName, $kDate, true );
  192. $kService = hash_hmac ( "sha256", $serviceName, $kRegion, true );
  193. $kSigning = hash_hmac ( "sha256", $this->aws4Request, $kService, true );
  194.  
  195. return $kSigning;
  196. }
  197.  
  198. private function getTimeStamp() {
  199. return gmdate ( "Ymd\THis\Z" );
  200. }
  201.  
  202. private function getDate() {
  203. return gmdate ( "Ymd" );
  204. }
  205. }
  206. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement