Get the Top Seller items from Amazon using AWS

In a previous post we had seen on how to access the Amazon Product Advertising API. In this post we will see how to add a function to get the ‘Top Sellers’ list for a particular category.

As mentioned in this post, Amazon uses a hierarchy of nodes to organize its various items. Each node represents a collection of items. Browse node IDs are positive integers that uniquely identify product collections. You can get a top seller list for a particular category or sub-category by passing a browsenode id for that particular category.

The following is a small snippet that you can add to the ‘AmazonProductAPI’ class to return the ‘Top Sellers’ list. You can download the support code from this post.

public function getTopSeller($browseNode)
{
    $parameters = array("Operation" => "BrowseNodeLookup",
                                       "BrowseNodeId" => $browseNode,
                                       "ResponseGroup" => "TopSellers");
 
    $xml_response = $this->queryAmazon($parameters);
 
    $top_seller = $xml_response->BrowseNodes->BrowseNode->TopSellers;
    $items = array();
 
    if(!empty($top_seller)) 
    {
        $top_list = $top_seller->TopSeller;
        if(count($top_list) > 0) 
        {
            foreach($top_list as $item) 
            {
                $items[(string)$item->ASIN] = (string)$item->Title;
            }
        }
    }
 
    return $items;
}

You can get the ‘Top Sellers’ list like the following.

$obj = new AmazonProductAPI();
$result = $obj->getTopSeller('172282');

The above returns the list for the browse node ‘172282’. Sometimes you might like to return the best sellers in the root browse node rather than a sub-category. To achieve that we will first need to get the browse node for the root category. The following is a function you can use to get the root browsenode id for a given sub-category browsenode.

public function getRootBrowseNodeID($browseNode)
{
    $parameters = array("Operation"     => "BrowseNodeLookup",
                                            "BrowseNodeId" => $browseNode);
 
    $xml_response = $this->queryAmazon($parameters);
    $nodeid = '';
    if(isset($xml_response->BrowseNodes->BrowseNode))
    {
        $anc = $xml_response->BrowseNodes->BrowseNode->Ancestors;
        while(isset($anc->BrowseNode->Ancestors))
        {
            $anc = $anc->BrowseNode->Ancestors;
            $nodeid = (string)$anc->BrowseNode->BrowseNodeId;
        }
    }
 
    return $nodeid;
}

For example the following returns the root browsenode id for the sub-category ‘comedy’.

$obj = new AmazonProductAPI();
$result = $obj->getRootBrowseNodeID('599826'); // Browsenode id for 'comedy'

The above function returns a single root browsenode id. However, if you look into the actual xml response you will see a long list of ids. The following is a internal partial response for the above query. More important to note, however, is the browse node ancestry that is returned. The further down the response that you read, the higher up you go in the browse node hierarchy. The eldest ancestor of the browse node ID, 599826, which is in the request is browse node ID 2625373011, ‘Movies & TV’. The ‘getRootBrowseNodeID’ function recursively parses the response and only returns the root id.

[BrowseNode] => SimpleXMLElement Object
(
    [BrowseNodeId] => 599826
    [Name] => Comedy
    [Ancestors] => SimpleXMLElement Object
        (
            [BrowseNode] => SimpleXMLElement Object
                (
                    [BrowseNodeId] => 501230
                    [Name] => Boxed Sets
                    [Ancestors] => SimpleXMLElement Object
                        (
                            [BrowseNode] => SimpleXMLElement Object
                                (
                                    [BrowseNodeId] => 2998369011
                                    [Name] => DVD Custom Stores
                                    [Ancestors] => SimpleXMLElement Object
                                        (
                                            [BrowseNode] => SimpleXMLElement Object
                                                (
                                                    [BrowseNodeId] => 2644982011
                                                    [Name] => Custom Stores
                                                    [Ancestors] => SimpleXMLElement Object
                                                        (
                                                            [BrowseNode] => SimpleXMLElement Object
                                                                (
                                                                    [BrowseNodeId] => 2644981011
                                                                    [Name] => Specialty Stores
                                                                    [Ancestors] => SimpleXMLElement Object
                                                                        (
                                                                            [BrowseNode] => SimpleXMLElement Object
                                                                                (
                                                                                    [BrowseNodeId] => 2625373011
                                                                                    [Name] => Movies & TV
                                                                                )
 
                                                                        )
 
                                                                )
 
                                                        )
 
                                                )
 
                                        )
 
                                )
 
                        )
 
                )
 
        )
 
)

Now that you have the browse node ID of the root product category, you can use the ‘getTopSeller’ function again, this time with the ‘TopSellers’ response group to return the top sellers in the root browse node category.



2 thoughts on “Get the Top Seller items from Amazon using AWS

  1. Thanks, Sameer, this is really helpful.
    What I was wondering: Is it possible to send an array of nodes as a request to the Amazon API? Let’s say, I’d like to fetch product information of all products in three sub categories of one main category PLUS only products with a price higher than 20 EUR. Do you happen to know if that’s possible?

Comments are closed.