In recent past, we had a requirement in our project where our client wanted us to show Amazon products on the site so that users can view Amazon products and add their favourite products to their cart which they can buy later during checkout process from Amazon. It helps us to attract users and increase user traffic to our site. This can be mostly helpful for sites having shopping cart facility, where we can list products added by site users as well as Amazon.com.
For this implementation Amazon.com provides an API named as Amazon Product Advertising API through which we can fetch information regarding Amazon.com products and show them on our site. The site owner can also get the benefit of a referral fees upto 15% of the product’s selling price for every purchase of Amazon.com products done via our website.
You need to create a Product Advertising API account at https://affiliate-program.amazon.com/gp/flex/advertising/api/sign-in.html to use Amazon.com API facility. After creating an account you will get an api key and a secret key associated with your account which you will need to use while fetching records from Amazon.com. To only list Amazon.com products on your site will need the API key, the Secret Key and below mentioned signature key but if you wish to get referral fees for referrals then you need to be part of the Amazon associates program. You can create an account for associates program at https://affiliate-program.amazon.com/.
After creating an account you will get an associate tag.
For authentication we need to generate a signature key for every request to Amazon.com. In PHP, we can generate signature by using sha256 and hmac. The PHP code for generating signature key can be accessed.
Amazon provides products for a number of categories like Books, DVD, Music etc. To get results from all categories a category named All can be used.We used SportingGoods category for our site as our site deals with Sports related products.
Building the request:
The request to fetch amazon product results can be built by using api key, secret key, signature key and associate tag(optional).
Then we need to calculate the signature key for every request.
Here is a sample request to fetch the products of Toys category with Rockets as keyword: $request = “http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=$api_key&AssociateTag=$associate_tag &Operation=ItemSearch&&ResponseGroup=Medium, Offers&Keywords=Rocket&SearchIndex=Toys&ItemPage=$ItemPage”; $request = getRequest($secret_key, $request, $api_key, “2009-03-01”); $session = curl_init($request); curl_setopt($session, CURLOPT_HEADER, false); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($session); curl_close($session);
$parsed_xml = simplexml_load_string($response);
This $parsed_xml gives us the amazon results and we need to show it in our site according to our requirement.
Cart Process:
Users can add the products they want to buy from Amazon.com to their cart.This carting process does not concern our database and is completely handled by Amazon.com.
There are various operations that can be performed during this process.Some examples are given below:
Operation Purpose CartCreate To create a new cart CartAdd To add a new item to a cart CartClear To clear the cart of all existing items. CartModify To change the quantity of items in the cart.
CartGet To get the details of every item in the cart.
CartGet request example: $request = http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=$api_key&AssociateTag=$associate_tag& Operation=CartGet&CartId=[Cart ID]&HMAC=[URL-encoded HMAC] Terminology: $api_key – Your Api Key $secret_key – Your Secret Key $associate_tag – Your Associate Tag Operation – The operation that you want perform ex:ItemSearch,ItemLookup,CartAdd,CartCreate etc. Response Group – The information that you require.Response Groups vary with the operation performed. A few examples are: Medium,Tracks,Tags etc. $Keywords – The keywords you want to search against. $SearchIndex – The category you are searching the results for. Few valid categories are All,Books,SportingGoods etc.
$ItemPage – The page number of the results set.One page consists of 10 records.
The whole developer guide can be accessed at:http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html
A sample PHP code with various operations can be found at :http://developer.amazonwebservices.com/connect/entry.jspa?externalID=498&categoryID=14
Hope this is useful!