/
amz.php
69 lines (53 loc) · 1.71 KB
/
amz.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
<?php
use ApaiIO\Configuration\GenericConfiguration;
use ApaiIO\Operations\Search;
use ApaiIO\Operations\BrowseNodeLookup;
use ApaiIO\Operations\Lookup;
use ApaiIO\ApaiIO;
require('vendor/autoload.php');
require('settings.php');
$conf = new GenericConfiguration;
$conf
->setCountry('com')
->setAccessKey(AWS_API_KEY)
->setSecretKey(AWS_API_SECRET_KEY)
->setAssociateTag(AWS_ASSOCIATE_TAG);
$apaiIO = new ApaiIO($conf);
$nodeId = 1; // Gotta start somewhere
$outputFile = fopen('hierarchy_' . str_pad($nodeId, 7, '0', STR_PAD_LEFT) . '.txt', 'w');
traverseNode(0, $nodeId, $apaiIO, $outputFile);
fclose($outputFile);
/**
* Recursive function for traversing down through all the decdendants of a
* specified Amazon BrowseNodeId
*/
function traverseNode($parentNodeId, $nodeId, &$apaiIO, &$outputFile)
{
// Don't rock the boat, keep it less than one request per second
// http://docs.aws.amazon.com/AWSECommerceService/latest/DG/TroubleshootingApplications.html
sleep(1.1);
$browse = new BrowseNodeLookup();
$browse
->setNodeId($nodeId)
->setResponseGroup(['BrowseNodeInfo']);
try {
$response = $apaiIO->runOperation($browse);
} catch (Exception $e) {
print_r($e->getMessage());
die();
}
$xml = simplexml_load_string($response);
$row = [
$parentNodeId,
$nodeId,
(string) $xml->BrowseNodes->BrowseNode->Name
];
fputcsv($outputFile, $row);
$children = $xml->BrowseNodes->BrowseNode->Children->BrowseNode;
if (count($children) > 0) {
foreach ($children as $childNode) {
// Recurse
traverseNode($nodeId, $childNode->BrowseNodeId, $apaiIO, $outputFile);
}
}
}