Tuesday, September 15, 2015

Amazon Advertising API BrowseNodes

Amazon uses a hierarchy of nodes to organize its various items. Each node represents a collection of items, such as Harry Potter books, not the items themselves. Browse node IDs are positive integers that uniquely identify product collections, for example, Literature & Fiction: (17), Medicine: (13996), Mystery & Thrillers: (18), Nonfiction: (53), Outdoors & Nature: (290060). Amazon uses over 120,000 browse node IDs in the US locale alone.
The BrowseNodes tool automatically gets all the child BrowseNodes and their names. The program recursively traverses the BrowserNode hierarchy and returns all the BrowseNodes and their respective names. You can display the nodes on the console or save it to a CSV file. You can also include the library in your existing projects to process BrowseNodes.

Library Usage

The library is easy to use, and requires PHP and CURL installed on your system. The download includes a sample file with which you can immediately test the code. The sample file is shown below.
<?php
 
/* Example usage of the Amazon Product Advertising API */
include("amazon_api_class.php");
 
$public_key     = "YOUR-AMAZON-PUBLIC-KEY";
$private_key    = "YOUR-AMAZON-SECRET-KEY";
$region         = "com"; // or "CA" or "DE" etc.
 
$obj = new AmazonProductAPI($public_key, $private_key, $region);
 
/* Display the BrowseNodes to the console */
$obj->setMedia("display");
 
$obj->getBrowseNodes("1266092011");
 
?>
Before you begin to use the library, you need to enter your Amazon security credentials as shown above. Also in the ‘aws_signed_request.php’ file you will need to enter your Amazon Associate ID, as it is now mandatory. Find the following line in the file and add your Associated ID.
/* aws_signed_request.php */
.
.
$params["AssociateTag"]     = 'YOUR-ASSOCIATES-ID-HERE';
.
That is it. Now you are ready to get the BrowserNodes from Amazon. You can display the BrowserNodes on you console/browser or save it directly to a CSV file. For that you have to use the ‘setMedia’ method. Both example are shown below.

Display the BrowseNodes on the console

<?php
 
/* Example usage of the Amazon Product Advertising API */
include("amazon_api_class.php");
 
$public_key     = "YOUR-AMAZON-PUBLIC-KEY";
$private_key    = "YOUR-AMAZON-SECRET-KEY";
$region         = "com"; // or "CA" or "DE" etc.
 
$obj = new AmazonProductAPI($public_key, $private_key, $region);
 
/* Display the BrowseNodes to the console */
$obj->setMedia("display");
 
$obj->getBrowseNodes("1266092011");
 
?>
You enter the parent browsenode in the ‘getBrowseNodes’ method, after which the function recursively retrieves all the children nodes. To get started, you can find all the top browsenode values here, or you can get it from the Amazon link itself.
The output of the above is shown below. The output is indented so that you can visualize the hierarchy of the nodes.
Television & Video : AV Receivers & Amplifiers : 3213035011
Television & Video : Blu-ray Players & Recorders : 3213025011
      Blu-ray Players & Recorders : Blu-ray Recorders : 3213026011
      Blu-ray Players & Recorders : Blu-ray Players : 352697011
Television & Video : DVD Players & Recorders : 3213027011
      DVD Players & Recorders : DVD Players : 1036922
      DVD Players & Recorders : DVD Recorders : 1036920
Television & Video : DVD-VCR Combos : 886258
Television & Video : HD DVD Players : 352696011
Television & Video : Televisions : 172659
Television & Video : Home Theater Systems : 281056
.
.
The format of each line is given below.

Saving the BrowseNodes to a CSV

The following code shows how to save the browsenodes to a CSV file.
<?php
 
/* Example usage of the Amazon Product Advertising API */
include("amazon_api_class.php");
 
$public_key     = "YOUR-AMAZON-PUBLIC-KEY";
$private_key    = "YOUR-AMAZON-SECRET-KEY";
$region         = "com"; // or "CA" or "DE" etc.
 
$obj = new AmazonProductAPI($public_key, $private_key, $region);
 
/* Write the BrowseNodes to a CSV file */
$obj->setMedia("csv", "./nodes.csv");
 
$obj->getBrowseNodes("1266092011");
 
?>
The code saves all the browsenodes in a CSV file, after which you can use it with Excel or a simple text editor. A sample CSV ouput is shown below. The format is the same as the one given above.
"PlayStation 3","All Games","14210861"
"PlayStation 3","Action","14210761"
"Action","Fighting","14210981"
"Action","Military & Espionage","14210821"
"Action","Shooter","14210841"
"PlayStation 3","Adventure","14210851"
"PlayStation 3","Arcade","14210911"
"PlayStation 3","Board Games","14210921"

Changing Locales

Amazon being a world-wide venture the Product Advertising API operates in the following locales:
CA
CN
DE
ES
FR
IT
JP
UK
US
You can use different locale code to get the BrowseNodes for different regions. You set the locale parameter using the ‘$region’ variable; for example:
$region = "com"; // or "CA" or "DE" etc.

Getting the parent node details

You can also get information about a nodes parent node using the ‘getParentNode’ method.
<?php
 
/* Example usage of the Amazon Product Advertising API */
include("amazon_api_class.php");
 
$public_key     = "YOUR-AMAZON-PUBLIC-KEY";
$private_key    = "YOUR-AMAZON-SECRET-KEY";
$region         = "com"; // or "CA" or "DE" etc.
 
$obj = new AmazonProductAPI($public_key, $private_key, $region);
 
print_r($obj->getParentNode("14210821"));
 
?>
One other useful method you can use it ‘getNodeName’. This allows you to get the name of a given node id. This can be useful if you have a nodeid but need to gets it name.
<?php
 
/* Example usage of the Amazon Product Advertising API */
include("amazon_api_class.php");
 
$public_key     = "YOUR-AMAZON-PUBLIC-KEY";
$private_key    = "YOUR-AMAZON-SECRET-KEY";
$region         = "com"; // or "CA" or "DE" etc.
 
$obj = new AmazonProductAPI($public_key, $private_key, $region);
 
echo $obj->getNodeName("14210821");
 
?>
Note: Some categories contain hundreds to thousands of BrowserNodes, retreiving them can take some time and could trigger a PHP timeout error. To avoid this set PHP timeout to a large number as given below.
<?php
 
set_time_limit(10000); 
 
/* Example usage of the Amazon Product Advertising API */
include("amazon_api_class.php");
.
.
?>
Download code files
Downloads : 2703 / File size : 3.1 kB http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/