-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathAmazonProductAPI.php
executable file
·459 lines (406 loc) · 11.9 KB
/
AmazonProductAPI.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
/**
* Class to access Amazons Product Advertising API
* @author Sameer Borate
* @link http://www.codediesel.com
* @version 1.0
* All requests are not implemented here. You can easily
* implement the others from the ones given below.
*/
/*
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/BasicAuthProcess.html
*/
namespace libs;
use nzedb\utility\Misc;
class AmazonProductAPI
{
/**
* Constants for product types
*
* @note More categories can be found here:
* http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/APPNDX_SearchIndexValues.html
*
* @var string
*/
const BOOKS = "Books";
const DIGITALMUS = "DigitalMusic";
const DVD = "DVD";
// This can be DigitalDownloads as well.
const GAMES = "VideoGames";
const MP3 = "MP3Downloads";
const MUSICTRACKS = "MusicTracks";
const MUSIC = "Music";
/**
* Your Amazon Access Key Id
* @access private
* @var string
*/
private $public_key = "";
/**
* Your Amazon Secret Access Key
* @access private
* @var string
*/
private $private_key = "";
/**
* Your Amazon Secret Associate Tag
* @access private
* @var string
*/
private $associate_tag = "";
/**
* The current search string.
* @var string
*/
private $searchString;
/**
* The current search category.
* @var string
*/
private $category;
/**
* The current search type.
* @var string
*/
private $searchType;
/**
* The current search node.
* @var string
*/
private $searchNode;
/**
* How many times have we tried to query amazon after being throttled
* @var int
*/
private $tries;
/**
* How many seconds must will we sleep currently while throttled.
* @var int
*/
private $currentSleepTime = 0;
/**
* Are we using this method currently?
* @var bool
*/
private $searchProducts = false;
/**
* How many times should we try to query after being throttled.
* @var int
*/
const maxTries = 3;
/**
* How many seconds should we wait after being throttled.
* @var int
*/
const sleepTime = 3;
/**
* Every time we are throttled, increase current sleep time by this much.
*/
const sleepIncrease = 1;
/**
* Construct.
*
* @param string $pubk Amazon public key.
* @param string $privk Amazon private key.
* @param string $associatetag Amazon associate tag.
*/
public function __construct($pubk, $privk, $associatetag)
{
$this->public_key = (string) $pubk;
$this->private_key = (string) $privk;
$this->associate_tag = (string) $associatetag;
$this->tries = 0;
$this->currentSleepTime = self::sleepTime;
}
/**
* Return details of products searched by various types
*
* @param string $search search term
* @param string $category search category
* @param string $searchType type of search
* @param string $searchNode
*
* @return bool|mixed
*/
public function searchProducts($search, $category, $searchType = "UPC", $searchNode="")
{
// Set class vars.
$this->searchString = $search;
$this->category = $category;
$this->searchType = $searchType;
$this->searchNode = $searchNode;
$this->searchProducts = true;
switch($searchType)
{
case "UPC" :
$parameters =
array(
"Operation" => "ItemLookup",
"ItemId" => $search,
"SearchIndex" => $category,
"IdType" => "UPC",
"ResponseGroup" => "Medium");
break;
case "ISBN" :
$parameters =
array(
"Operation" => "ItemLookup",
"ItemId" => $search,
"SearchIndex" => self::BOOKS,
"IdType" => "ISBN",
"ResponseGroup" => "Medium"
);
break;
case "TITLE" :
switch($category)
{
case "MUSICTRACKS" :
$parameters =
array(
"Operation" => "ItemSearch",
//"Title" => $search,
"Keywords" => $search,
"Sort" => "titlerank",
"SearchIndex" => $category,
"ResponseGroup" => "Large"
);
break;
default :
$parameters =
array(
"Operation" => "ItemSearch",
//"Title" => $search,
"Keywords" => $search,
"Sort" => "relevancerank",
"SearchIndex" => $category,
"ResponseGroup" => "Large"
);
break;
}
break;
case "TITLE2" :
$parameters =
array(
"Operation" => "ItemSearch",
"Title" => $search,
//"Keywords" => $search,
"Sort" => "relevancerank",
"SearchIndex" => $category,
"ResponseGroup" => "Large"
);
break;
// Same as TITLE but add BrowseNodeID param.
case "NODE" :
$parameters =
array(
"Operation" => "ItemSearch",
//"Title" => $search,
"Keywords" => $search,
"SearchIndex" => $category,
"BrowseNode" => $searchNode,
"ResponseGroup" => "Large"
);
break;
}
return $this->verifyXmlResponse($this->queryAmazon($parameters));
}
/**
* Return details of a product searched by UPC
*
* @param int $upc_code UPC code of the product to search
* @param string $product_type type of the product
* @return mixed simpleXML object
*/
public function getItemByUpc($upc_code, $product_type)
{
$parameters =
array(
"Operation" => "ItemLookup",
"ItemId" => $upc_code,
"SearchIndex" => $product_type,
"IdType" => "UPC",
"ResponseGroup" => "Medium"
);
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
/**
* Return details of a product searched by ASIN.
*
* @param int $asin_code ASIN code of the product to search
* @param string $region Domain name extension (com, ca, etc).
*
* @return bool|mixed
*/
public function getItemByAsin($asin_code, $region = "com")
{
$parameters =
array(
"Operation" => "ItemLookup",
"ItemId" => $asin_code,
"ResponseGroup" => "Medium"
);
$xml_response = $this->queryAmazon($parameters, $region);
return $this->verifyXmlResponse($xml_response);
}
/**
* Return details of a product searched by keyword
*
* @param string $keyword keyword to search
* @param string $product_type type of the product
* @return mixed simpleXML object
*/
public function getItemByKeyword($keyword, $product_type)
{
$parameters =
array(
"Operation" => "ItemSearch",
"Keywords" => $keyword,
"SearchIndex" => $product_type
);
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
/**
* Reset some class object variables.
* @void
*/
private function resetVars()
{
$this->currentSleepTime = self::sleepTime;
$this->tries = 0;
$this->searchProducts = false;
}
/**
* Check if the xml received from Amazon is valid
*
* @param mixed $response xml response to check
*
* @return bool|mixed false if the xml is invalid, mixed if the xml response if it is valid
* @throws exception if we could not connect to Amazon
*/
private function verifyXmlResponse($response)
{
// Check if there's an error.
if (isset($response->Error)) {
// Check if we are throttled.
if ($this->searchProducts && strpos(strtolower($response->Error->Message), 'slower rate') !== false && $this->tries <= self::maxTries) {
// Sleep to let the throttle wear off.
sleep($this->currentSleepTime);
// Increase next sleep time.
$this->currentSleepTime += self::sleepIncrease;
// Increment current tries.
$this->tries++;
// Try again.
return $this->searchProducts($this->searchString, $this->category, $this->searchType, $this->searchNode);
}
// Echo the message.
echo $response->Error->Message . "\n";
$this->resetVars();
throw new \Exception($response->Error->Message);
} else if ($response === False) {
$this->resetVars();
throw new \Exception("Could not connect to Amazon.");
} else if ($response == "missingkey") {
$this->resetVars();
throw new \Exception("Missing Amazon API key or associate tag.");
} else {
if (isset($response->Items->Item->ItemAttributes->Title)) {
$this->resetVars();
return ($response);
} else {
$this->resetVars();
throw new \Exception("Invalid xml response.");
}
}
}
/**
* Query Amazon with the issued parameters
*
* @param array $parameters parameters to query around
* @param string $region Domain name extension (com, ca, etc).
*
* @return bool|SimpleXMLElement|string xml query response
*/
private function queryAmazon($parameters, $region = "com")
{
return $this->aws_signed_request($region, $parameters, $this->public_key, $this->private_key, $this->associate_tag);
}
/**
* @param $region
* @param $params
* @param $public_key
* @param $private_key
* @param string $associate_tag
*
* @return SimpleXMLElement|false|string
*/
private function aws_signed_request($region, $params, $public_key, $private_key, $associate_tag = "")
{
if ($public_key !== "" && $private_key !== "" && $associate_tag !== "")
{
$method = "GET";
// Must be in small case.
$host = "ecs.amazonaws.".$region;
$uri = "/onca/xml";
$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
$params["AssociateTag"] = $associate_tag;
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
$params["Version"] = "2009-03-31";
/* The params need to be sorted by the key, as Amazon does this at
their end and then generates the hash of the same. If the params
are not in order then the generated hash will be different thus
failing the authetication process.
*/
ksort($params);
$canonicalized_query = array();
foreach ($params as $param=>$value)
{
$param = str_replace("%7E", "~", rawurlencode($param));
$value = str_replace("%7E", "~", rawurlencode($value));
$canonicalized_query[] = $param."=".$value;
}
$canonicalized_query = implode("&", $canonicalized_query);
$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
/* Calculate the signature using HMAC with SHA256 and base64-encoding.
* The 'hash_hmac' function is only available from PHP 5 >= 5.1.2.
*/
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
// Encode the signature for the request.
$signature = str_replace("%7E", "~", rawurlencode($signature));
// Create request.
$request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt_array($ch, Misc::curlSslContextOptions());
$xml_response = curl_exec($ch);
if ($xml_response === False) {
return False;
} else {
// Parse XML.
$parsed_xml = @simplexml_load_string($xml_response);
return ($parsed_xml === False) ? False : $parsed_xml;
}
} else {
return "missingkey";
}
}
}