Resources for Developers Using Amazon's Product Advertising API
· Home  
· Search  
· Browse Nodes  
· Data Feed?  
· FAQs  
· One-Second Rule  
· PHP Examples  
· Tips for Associates  
· Tools  
· Understanding A2S  

Create a custom Amazon Associate Store in minutes with Associate-O-Matic.

clixGalores Merchant & Affiliate Referral Commission Program

A Simple Way to Determine the Search Index for a Browse Node

The following PHP code illustrates a simple method for determining an appropriate search index to use for a given browse node ID. The getIndex function is passed the browse node ID and returns the search index as a string (or '-unknown-' if the browse node ID is invalid or the code is unable to determine a search index to use).

<?php

//***********************************************
// PHP A2S Demo
//
// A demonstration of a simple method for
// determining a good search index to
// use for a specified browse node.
//
// Roger Smith
// a2sdeveloper.com
//***********************************************

// A brief name for your site
$sitename = "Test";

// The URL of your site
$siteurl = "http://www.yoursite.com";

// AWS Access Key Id
$awsaccesskey = "144RR0T0YM45X6SVKSR2";

// A2S Version to Use
$version = "2100-01-01";

//***********************************************
//Alternate versions of getPage, including
// one that works without requiring curl
// and one that caches information can be
// found at: 
//
//   http://www.a2sdeveloper.com/
//      page-php-examples.html
//
//***********************************************
function getPage($url)
 {
  global $sitename, $siteurl;

  $ch = curl_init(); 
  curl_setopt ($ch, CURLOPT_URL, $url); 
  curl_setopt ($ch, CURLOPT_USERAGENT, "$sitename/1.0 (+$siteurl)"); 
  curl_setopt ($ch, CURLOPT_HEADER, 1); 
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
  $result = curl_exec ($ch);
  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  $hsize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  $result = substr($result, $hsize);
  curl_close($ch);

  // echo $result . "<p>";

  return $result; 
 }

//***********************************************
// getIndex is the function being
// demonstrated. It takes a browse node
// ID and returns a good search index to
// use (or '-unknown-' for no search index).
//***********************************************
function getIndex($node)
 {
  global $awsaccesskey, $version;

  // Array of "root" nodes and indexes
  // derived from BrowseNodes.com

  $root[1036592] = 'Apparel';
  $root[672123011] = 'Apparel';
  $root[165796011] = 'Baby';
  $root[283155] = 'Books';
  $root[85] = 'Classical';
  $root[320031011] = 'Classical';
  $root[130] = 'DVD';
  $root[172282] = 'Electronics';
  $root[3370831] = 'GourmetFood';
  $root[12923371] = 'Grocery';
  $root[16014831] = 'Grocery';
  $root[16310101] = 'Grocery';
  $root[3760911] = 'HealthPersonalCare';
  $root[3760901] = 'HealthPersonalCare';
  $root[123382011] = 'HealthPersonalCare';
  $root[286168] = 'HomeGarden';
  $root[16310091] = 'Industrial';
  $root[10368791] = 'Industrial';
  $root[3367581] = 'Jewelry';
  $root[133140011] = 'KindleStore';
  $root[133141011] = 'KindleStore';
  $root[16310331] = 'Kitchen';
  $root[1055398] = 'Kitchen';
  $root[284507] = 'Kitchen';
  $root[599858] = 'Magazines';
  $root[349027011] = 'Merchants';
  $root[10963061] = 'Merchants';
  $root[265523] = 'Merchants';
  $root[3489301] = 'Merchants';
  $root[163856011] = 'MP3Downloads';
  $root[388864011] = 'Music';
  $root[5174] = 'Music';
  $root[734368] = 'Music';
  $root[11091801] = 'MusicalInstruments';
  $root[11965861] = 'MusicalInstruments';
  $root[1064954] = 'OfficeProducts';
  $root[1064952] = 'OfficeProducts';
  $root[13900831] = 'OutdoorLiving';
  $root[573752] = 'PCHardware';
  $root[13458241] = 'PCHardware';
  $root[13900871] = 'PCHardware';
  $root[229534] = 'Software';
  $root[3386071] = 'SportingGoods';
  $root[3564781] = 'SportingGoods';
  $root[3489461] = 'SportingGoods';
  $root[3375251] = 'SportingGoods';
  $root[228013] = 'Tools';
  $root[735342] = 'Tools';
  $root[15706941] = 'Tools';
  $root[165793011] = 'Toys';
  $root[165795011] = 'Toys';
  $root[165994011] = 'Toys';
  $root[166644011] = 'Toys';
  $root[375519011] = 'Toys';
  $root[16261641] = 'UnboxVideo';
  $root[16261631] = 'UnboxVideo';
  $root[404272] = 'VHS';
  $root[468642] = 'VideoGames';
  $root[377110011] = 'Watches';
  $root[3888811] = 'Watches';
  $root[301185] = 'Wireless';
  $root[301188] = 'WirelessAccessories';

  // Get data from A2S
  $url = "http://ecs.amazonaws.com/onca/xml";
  $url .= "?Service=AWSECommerceService";
  $url .= "&AWSAccessKeyId=$awsaccesskey";
  $url .= "&Operation=BrowseNodeLookup";
  $url .= "&Version=$version";
  $url .= "&BrowseNodeId=$node";

  $result = getPage($url);

  $index = '-unknown-';

  if ($result != '')
    {
     $parser = xml_parser_create();
     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING,0);
     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE,1);
     xml_parse_into_struct($parser, $result, $values, $tags);
     xml_parser_free($parser);
     
     // Simple parsing ... we just look at every BrowseNodeId element
     foreach ($values as $key=>$val) 
      {
       switch ($val['tag'])
         {
          case 'BrowseNodeId':
           $nodeId = $val['value'];

           // echo "$nodeId<br>";

           // If the ID we've found is a "root" node,
           // return the search index for that node.

           if (isset($root[$nodeId]))
             {
              $index = $root[$nodeId];
              break 2;
             }
          break;
         }
      }
    }
  return $index;
 }

if ($node != '')
  {
   $index = getIndex($node);
   echo "The index of node $node is $index.<p>";
  }

echo <<<END_FORM
     <form action="getindex.php" method="get">
       Node: <input type="text" name="node" size="10"> 
       <input type="submit">
     </form>
END_FORM;

?>


Copyright © 2010 by Roger Smith