-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.php
273 lines (239 loc) · 9.92 KB
/
create.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
/* Licensed under the Apache License, Version 2.0. */
$var_value = $_GET['asin_code'];
$serviceName="ProductAdvertisingAPI";
$region="eu-west-1";
$accessKey="YOUR-AMAZON-ACCESS-KEY";
$secretKey="YOUR-AMAZON-SECRET-KEY";
$partnerTag="ASSOCIATE-TAG";
$payload="{"
." \"ItemIds\": ["
." \"".$var_value."\""
." ],"
." \"Resources\": ["
." \"BrowseNodeInfo.BrowseNodes\","
." \"BrowseNodeInfo.BrowseNodes.Ancestor\","
." \"BrowseNodeInfo.BrowseNodes.SalesRank\","
." \"BrowseNodeInfo.WebsiteSalesRank\","
." \"CustomerReviews.Count\","
." \"CustomerReviews.StarRating\","
." \"Images.Primary.Small\","
." \"Images.Primary.Medium\","
." \"Images.Primary.Large\","
." \"Images.Variants.Small\","
." \"Images.Variants.Medium\","
." \"Images.Variants.Large\","
." \"ItemInfo.ByLineInfo\","
." \"ItemInfo.ContentInfo\","
." \"ItemInfo.ContentRating\","
." \"ItemInfo.Classifications\","
." \"ItemInfo.ExternalIds\","
." \"ItemInfo.Features\","
." \"ItemInfo.ManufactureInfo\","
." \"ItemInfo.ProductInfo\","
." \"ItemInfo.TechnicalInfo\","
." \"ItemInfo.Title\","
." \"ItemInfo.TradeInInfo\","
." \"Offers.Listings.Availability.MaxOrderQuantity\","
." \"Offers.Listings.Availability.Message\","
." \"Offers.Listings.Availability.MinOrderQuantity\","
." \"Offers.Listings.Availability.Type\","
." \"Offers.Listings.Condition\","
." \"Offers.Listings.Condition.ConditionNote\","
." \"Offers.Listings.Condition.SubCondition\","
." \"Offers.Listings.DeliveryInfo.IsAmazonFulfilled\","
." \"Offers.Listings.DeliveryInfo.IsFreeShippingEligible\","
." \"Offers.Listings.DeliveryInfo.IsPrimeEligible\","
." \"Offers.Listings.DeliveryInfo.ShippingCharges\","
." \"Offers.Listings.IsBuyBoxWinner\","
." \"Offers.Listings.LoyaltyPoints.Points\","
." \"Offers.Listings.MerchantInfo\","
." \"Offers.Listings.Price\","
." \"Offers.Listings.ProgramEligibility.IsPrimeExclusive\","
." \"Offers.Listings.ProgramEligibility.IsPrimePantry\","
." \"Offers.Listings.Promotions\","
." \"Offers.Listings.SavingBasis\","
." \"Offers.Summaries.HighestPrice\","
." \"Offers.Summaries.LowestPrice\","
." \"Offers.Summaries.OfferCount\","
." \"ParentASIN\","
." \"RentalOffers.Listings.Availability.MaxOrderQuantity\","
." \"RentalOffers.Listings.Availability.Message\","
." \"RentalOffers.Listings.Availability.MinOrderQuantity\","
." \"RentalOffers.Listings.Availability.Type\","
." \"RentalOffers.Listings.BasePrice\","
." \"RentalOffers.Listings.Condition\","
." \"RentalOffers.Listings.Condition.ConditionNote\","
." \"RentalOffers.Listings.Condition.SubCondition\","
." \"RentalOffers.Listings.DeliveryInfo.IsAmazonFulfilled\","
." \"RentalOffers.Listings.DeliveryInfo.IsFreeShippingEligible\","
." \"RentalOffers.Listings.DeliveryInfo.IsPrimeEligible\","
." \"RentalOffers.Listings.DeliveryInfo.ShippingCharges\","
." \"RentalOffers.Listings.MerchantInfo\""
." ],"
." \"PartnerTag\": \"".$partnerTag."\","
." \"PartnerType\": \"Associates\","
." \"Marketplace\": \"www.amazon.in\""
."}";
$host="webservices.amazon.in";
$uriPath="/paapi5/getitems";
$awsv4 = new AwsV4 ($accessKey, $secretKey);
$awsv4->setRegionName($region);
$awsv4->setServiceName($serviceName);
$awsv4->setPath ($uriPath);
$awsv4->setPayload ($payload);
$awsv4->setRequestMethod ("POST");
$awsv4->addHeader ('content-encoding', 'amz-1.0');
$awsv4->addHeader ('content-type', 'application/json; charset=utf-8');
$awsv4->addHeader ('host', $host);
$awsv4->addHeader ('x-amz-target', 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems');
$headers = $awsv4->getHeaders ();
$headerString = "";
foreach ( $headers as $key => $value ) {
$headerString .= $key . ': ' . $value . "\r\n";
}
$params = array (
'http' => array (
'header' => $headerString,
'method' => 'POST',
'content' => $payload
)
);
$stream = stream_context_create ( $params );
$fp = @fopen ( 'https://'.$host.$uriPath, 'rb', false, $stream );
if (! $fp) {
throw new Exception ( "Exception Occured" );
}
$response = @stream_get_contents ( $fp );
if ($response === false) {
throw new Exception ( "Exception Occured" );
}
$itemResult = json_decode($response);
$price = str_replace(".", "", $itemResult->ItemsResult->Items[0]->Offers->Listings[0]->Price->Amount);
$i = $itemResult->ItemsResult->Items[0];
$item = new STDclass;
$item->detailedPageURL = $i->DetailPageURL;
$item->title = $i->ItemInfo->Title->DisplayValue;
$item->smallImage = $i->Images->Primary->Small->URL;
$item->mediumImage = $i->Images->Primary->Medium->URL;
$item->largeImage = $i->Images->Primary->Large->URL;
$item->percentage = $i->Offers->Listings[0]->Price->Savings->Percentage;
$item->price = $i->Offers->Listings[0]->Price->DisplayAmount;
$item->Realprice = $i->Offers->Listings[0]->SavingBasis->DisplayAmount;
$item->features = $i->ItemInfo->Features->DisplayValues;
$item->ratings = $i->Offers->Listings[0]->MerchantInfo->FeedbackRating;
class AwsV4 {
private $accessKey = null;
private $secretKey = null;
private $path = null;
private $regionName = null;
private $serviceName = null;
private $httpMethodName = null;
private $queryParametes = array ();
private $awsHeaders = array ();
private $payload = "";
private $HMACAlgorithm = "AWS4-HMAC-SHA256";
private $aws4Request = "aws4_request";
private $strSignedHeader = null;
private $xAmzDate = null;
private $currentDate = null;
public function __construct($accessKey, $secretKey) {
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
$this->xAmzDate = $this->getTimeStamp ();
$this->currentDate = $this->getDate ();
}
function setPath($path) {
$this->path = $path;
}
function setServiceName($serviceName) {
$this->serviceName = $serviceName;
}
function setRegionName($regionName) {
$this->regionName = $regionName;
}
function setPayload($payload) {
$this->payload = $payload;
}
function setRequestMethod($method) {
$this->httpMethodName = $method;
}
function addHeader($headerName, $headerValue) {
$this->awsHeaders [$headerName] = $headerValue;
}
private function prepareCanonicalRequest() {
$canonicalURL = "";
$canonicalURL .= $this->httpMethodName . "\n";
$canonicalURL .= $this->path . "\n" . "\n";
$signedHeaders = '';
foreach ( $this->awsHeaders as $key => $value ) {
$signedHeaders .= $key . ";";
$canonicalURL .= $key . ":" . $value . "\n";
}
$canonicalURL .= "\n";
$this->strSignedHeader = substr ( $signedHeaders, 0, - 1 );
$canonicalURL .= $this->strSignedHeader . "\n";
$canonicalURL .= $this->generateHex ( $this->payload );
return $canonicalURL;
}
private function prepareStringToSign($canonicalURL) {
$stringToSign = '';
$stringToSign .= $this->HMACAlgorithm . "\n";
$stringToSign .= $this->xAmzDate . "\n";
$stringToSign .= $this->currentDate . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "\n";
$stringToSign .= $this->generateHex ( $canonicalURL );
return $stringToSign;
}
private function calculateSignature($stringToSign) {
$signatureKey = $this->getSignatureKey ( $this->secretKey, $this->currentDate, $this->regionName, $this->serviceName );
$signature = hash_hmac ( "sha256", $stringToSign, $signatureKey, true );
$strHexSignature = strtolower ( bin2hex ( $signature ) );
return $strHexSignature;
}
public function getHeaders() {
$this->awsHeaders ['x-amz-date'] = $this->xAmzDate;
ksort ( $this->awsHeaders );
// Step 1: CREATE A CANONICAL REQUEST
$canonicalURL = $this->prepareCanonicalRequest ();
// Step 2: CREATE THE STRING TO SIGN
$stringToSign = $this->prepareStringToSign ( $canonicalURL );
// Step 3: CALCULATE THE SIGNATURE
$signature = $this->calculateSignature ( $stringToSign );
// Step 4: CALCULATE AUTHORIZATION HEADER
if ($signature) {
$this->awsHeaders ['Authorization'] = $this->buildAuthorizationString ( $signature );
return $this->awsHeaders;
}
}
private function buildAuthorizationString($strSignature) {
return $this->HMACAlgorithm . " " . "Credential=" . $this->accessKey . "/" . $this->getDate () . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "," . "SignedHeaders=" . $this->strSignedHeader . "," . "Signature=" . $strSignature;
}
private function generateHex($data) {
return strtolower ( bin2hex ( hash ( "sha256", $data, true ) ) );
}
private function getSignatureKey($key, $date, $regionName, $serviceName) {
$kSecret = "AWS4" . $key;
$kDate = hash_hmac ( "sha256", $date, $kSecret, true );
$kRegion = hash_hmac ( "sha256", $regionName, $kDate, true );
$kService = hash_hmac ( "sha256", $serviceName, $kRegion, true );
$kSigning = hash_hmac ( "sha256", $this->aws4Request, $kService, true );
return $kSigning;
}
private function getTimeStamp() {
return gmdate ( "Ymd\THis\Z" );
}
private function getDate() {
return gmdate ( "Ymd" );
}
}
?>
<a href="<?php echo $item->detailedPageURL?>" title="<?php echo $item->title?>" target="_blank"></a>
<img src="<?php echo $item->mediumImage?>" alt="<?php echo $item->title?>">
<span><?php echo $item->percentage?>%</span><br>
<a href="<?php echo $item->detailedPageURL?>" title="<?php echo $item->title?>" target="_blank"><?php echo $item->title?></a>
<br>
<span><?php echo $item->price?></span>
<?php foreach ($item->features as $key => $value) :?>
<p><?php echo $value."<br>"?></p>
<?php endforeach?>