· 5 years ago · Mar 24, 2020, 04:56 PM
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="**********";
10$secretKey="**********";
11$payload="{"
12 ." \"Keywords\": \"B081WT2QSW\","
13 ." \"Resources\": ["
14 ." \"BrowseNodeInfo.BrowseNodes\","
15 ." \"BrowseNodeInfo.BrowseNodes.Ancestor\","
16 ." \"BrowseNodeInfo.BrowseNodes.SalesRank\","
17 ." \"BrowseNodeInfo.WebsiteSalesRank\","
18 ." \"CustomerReviews.Count\","
19 ." \"CustomerReviews.StarRating\","
20 ." \"Images.Primary.Small\","
21 ." \"Images.Primary.Medium\","
22 ." \"Images.Primary.Large\","
23 ." \"Images.Variants.Small\","
24 ." \"Images.Variants.Medium\","
25 ." \"Images.Variants.Large\","
26 ." \"ItemInfo.ByLineInfo\","
27 ." \"ItemInfo.ContentInfo\","
28 ." \"ItemInfo.ContentRating\","
29 ." \"ItemInfo.Classifications\","
30 ." \"ItemInfo.ExternalIds\","
31 ." \"ItemInfo.Features\","
32 ." \"ItemInfo.ManufactureInfo\","
33 ." \"ItemInfo.ProductInfo\","
34 ." \"ItemInfo.TechnicalInfo\","
35 ." \"ItemInfo.Title\","
36 ." \"ItemInfo.TradeInInfo\","
37 ." \"Offers.Listings.Availability.MaxOrderQuantity\","
38 ." \"Offers.Listings.Availability.Message\","
39 ." \"Offers.Listings.Availability.MinOrderQuantity\","
40 ." \"Offers.Listings.Availability.Type\","
41 ." \"Offers.Listings.Condition\","
42 ." \"Offers.Listings.Condition.SubCondition\","
43 ." \"Offers.Listings.DeliveryInfo.IsAmazonFulfilled\","
44 ." \"Offers.Listings.DeliveryInfo.IsFreeShippingEligible\","
45 ." \"Offers.Listings.DeliveryInfo.IsPrimeEligible\","
46 ." \"Offers.Listings.DeliveryInfo.ShippingCharges\","
47 ." \"Offers.Listings.IsBuyBoxWinner\","
48 ." \"Offers.Listings.LoyaltyPoints.Points\","
49 ." \"Offers.Listings.MerchantInfo\","
50 ." \"Offers.Listings.Price\","
51 ." \"Offers.Listings.ProgramEligibility.IsPrimeExclusive\","
52 ." \"Offers.Listings.ProgramEligibility.IsPrimePantry\","
53 ." \"Offers.Listings.Promotions\","
54 ." \"Offers.Listings.SavingBasis\","
55 ." \"Offers.Summaries.HighestPrice\","
56 ." \"Offers.Summaries.LowestPrice\","
57 ." \"Offers.Summaries.OfferCount\","
58 ." \"ParentASIN\","
59 ." \"RentalOffers.Listings.Availability.MaxOrderQuantity\","
60 ." \"RentalOffers.Listings.Availability.Message\","
61 ." \"RentalOffers.Listings.Availability.MinOrderQuantity\","
62 ." \"RentalOffers.Listings.Availability.Type\","
63 ." \"RentalOffers.Listings.BasePrice\","
64 ." \"RentalOffers.Listings.Condition\","
65 ." \"RentalOffers.Listings.Condition.SubCondition\","
66 ." \"RentalOffers.Listings.DeliveryInfo.IsAmazonFulfilled\","
67 ." \"RentalOffers.Listings.DeliveryInfo.IsFreeShippingEligible\","
68 ." \"RentalOffers.Listings.DeliveryInfo.IsPrimeEligible\","
69 ." \"RentalOffers.Listings.DeliveryInfo.ShippingCharges\","
70 ." \"RentalOffers.Listings.MerchantInfo\","
71 ." \"SearchRefinements\""
72 ." ],"
73 ." \"PartnerTag\": \"**********","
74 ." \"PartnerType\": \"Associates\","
75 ." \"Marketplace\": \"www.amazon.it\""
76 ."}";
77$host="webservices.amazon.it";
78$uriPath="/paapi5/searchitems";
79$awsv4 = new AwsV4 ($accessKey, $secretKey);
80$awsv4->setRegionName($region);
81$awsv4->setServiceName($serviceName);
82$awsv4->setPath ($uriPath);
83$awsv4->setPayload ($payload);
84$awsv4->setRequestMethod ("POST");
85$awsv4->addHeader ('content-encoding', 'amz-1.0');
86$awsv4->addHeader ('content-type', 'application/json; charset=utf-8');
87$awsv4->addHeader ('host', $host);
88$awsv4->addHeader ('x-amz-target', 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems');
89$headers = $awsv4->getHeaders ();
90$headerString = "";
91foreach ( $headers as $key => $value ) {
92 $headerString .= $key . ': ' . $value . "\r\n";
93}
94$params = array (
95 'http' => array (
96 'header' => $headerString,
97 'method' => 'POST',
98 'content' => $payload
99 )
100 );
101$stream = stream_context_create ( $params );
102
103$fp = @fopen ( 'https://'.$host.$uriPath, 'rb', false, $stream );
104
105if (! $fp) {
106 throw new Exception ( "Exception Occured" );
107}
108$response = @stream_get_contents ( $fp );
109if ($response === false) {
110 throw new Exception ( "Exception Occured" );
111}
112echo $response;
113
114class AwsV4 {
115
116 private $accessKey = null;
117 private $secretKey = null;
118 private $path = null;
119 private $regionName = null;
120 private $serviceName = null;
121 private $httpMethodName = null;
122 private $queryParametes = array ();
123 private $awsHeaders = array ();
124 private $payload = "";
125
126 private $HMACAlgorithm = "AWS4-HMAC-SHA256";
127 private $aws4Request = "aws4_request";
128 private $strSignedHeader = null;
129 private $xAmzDate = null;
130 private $currentDate = null;
131
132 public function __construct($accessKey, $secretKey) {
133 $this->accessKey = $accessKey;
134 $this->secretKey = $secretKey;
135 $this->xAmzDate = $this->getTimeStamp ();
136 $this->currentDate = $this->getDate ();
137 }
138
139 function setPath($path) {
140 $this->path = $path;
141 }
142
143 function setServiceName($serviceName) {
144 $this->serviceName = $serviceName;
145 }
146
147 function setRegionName($regionName) {
148 $this->regionName = $regionName;
149 }
150
151 function setPayload($payload) {
152 $this->payload = $payload;
153 }
154
155 function setRequestMethod($method) {
156 $this->httpMethodName = $method;
157 }
158
159 function addHeader($headerName, $headerValue) {
160 $this->awsHeaders [$headerName] = $headerValue;
161 }
162
163 private function prepareCanonicalRequest() {
164 $canonicalURL = "";
165 $canonicalURL .= $this->httpMethodName . "\n";
166 $canonicalURL .= $this->path . "\n" . "\n";
167 $signedHeaders = '';
168 foreach ( $this->awsHeaders as $key => $value ) {
169 $signedHeaders .= $key . ";";
170 $canonicalURL .= $key . ":" . $value . "\n";
171 }
172 $canonicalURL .= "\n";
173 $this->strSignedHeader = substr ( $signedHeaders, 0, - 1 );
174 $canonicalURL .= $this->strSignedHeader . "\n";
175 $canonicalURL .= $this->generateHex ( $this->payload );
176 return $canonicalURL;
177 }
178
179 private function prepareStringToSign($canonicalURL) {
180 $stringToSign = '';
181 $stringToSign .= $this->HMACAlgorithm . "\n";
182 $stringToSign .= $this->xAmzDate . "\n";
183 $stringToSign .= $this->currentDate . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "\n";
184 $stringToSign .= $this->generateHex ( $canonicalURL );
185 return $stringToSign;
186 }
187
188 private function calculateSignature($stringToSign) {
189 $signatureKey = $this->getSignatureKey ( $this->secretKey, $this->currentDate, $this->regionName, $this->serviceName );
190 $signature = hash_hmac ( "sha256", $stringToSign, $signatureKey, true );
191 $strHexSignature = strtolower ( bin2hex ( $signature ) );
192 return $strHexSignature;
193 }
194
195 public function getHeaders() {
196 $this->awsHeaders ['x-amz-date'] = $this->xAmzDate;
197 ksort ( $this->awsHeaders );
198
199 // Step 1: CREATE A CANONICAL REQUEST
200 $canonicalURL = $this->prepareCanonicalRequest ();
201
202 // Step 2: CREATE THE STRING TO SIGN
203 $stringToSign = $this->prepareStringToSign ( $canonicalURL );
204
205 // Step 3: CALCULATE THE SIGNATURE
206 $signature = $this->calculateSignature ( $stringToSign );
207
208 // Step 4: CALCULATE AUTHORIZATION HEADER
209 if ($signature) {
210 $this->awsHeaders ['Authorization'] = $this->buildAuthorizationString ( $signature );
211 return $this->awsHeaders;
212 }
213 }
214
215 private function buildAuthorizationString($strSignature) {
216 return $this->HMACAlgorithm . " " . "Credential=" . $this->accessKey . "/" . $this->getDate () . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "," . "SignedHeaders=" . $this->strSignedHeader . "," . "Signature=" . $strSignature;
217 }
218
219 private function generateHex($data) {
220 return strtolower ( bin2hex ( hash ( "sha256", $data, true ) ) );
221 }
222
223 private function getSignatureKey($key, $date, $regionName, $serviceName) {
224 $kSecret = "AWS4" . $key;
225 $kDate = hash_hmac ( "sha256", $date, $kSecret, true );
226 $kRegion = hash_hmac ( "sha256", $regionName, $kDate, true );
227 $kService = hash_hmac ( "sha256", $serviceName, $kRegion, true );
228 $kSigning = hash_hmac ( "sha256", $this->aws4Request, $kService, true );
229
230 return $kSigning;
231 }
232
233 private function getTimeStamp() {
234 return gmdate ( "Ymd\THis\Z" );
235 }
236
237 private function getDate() {
238 return gmdate ( "Ymd" );
239 }
240}
241?>