Updated: 19th October 2011
In this post we will see how to access the Amazon Product Advertising API from PHP. Amazon has recently changed (from 15th Aug ’09) the authentication mechanism for accessing their API which must now be signed with your Amazon keys. Unsigned requests will be rejected by Amazon. Also now Amazon Associate Tag is required in the query (effective from 25th Oct. 2011). Note that the code uses the hash_hmac() hash function which is only available for PHP versions 5.1.2 and above, so the code will not work for versions below that.
A small example
Below is an example to access the Amazon Product Advertising API using the provided class.
searchProducts("X-Men Origins",
AmazonProductAPI::DVD,
"TITLE");
}
catch(Exception $e)
{
echo $e->getMessage();
}
print_r($result);
?>
You can convert the returned xml to json with the following line.
$result = json_encode($result);
The API access class
Given below is the implementation of the class to access the Amazon Product Advertising API. Comments have been removed for brevity, but are included in the source download. Note that only a few access operations are implemented in the class: ItemLookup, ItemSearch; there are more available in the API, a complete list can be found here. It’s just a simple matter of changing some parameters to implement others.
Items->Item->ItemAttributes->Title))
{
return ($response);
}
else
{
throw new Exception("Invalid xml response.");
}
}
}
private function queryAmazon($parameters)
{
return aws_signed_request("com",
$parameters,
$this->public_key,
$this->private_key,
$this->associate_tag);
}
public function searchProducts($search,$category,$searchType="UPC")
{
$allowedTypes = array("UPC", "TITLE", "ARTIST", "KEYWORD");
$allowedCategories = array("Music", "DVD", "VideoGames");
switch($searchType)
{
case "UPC" :
$parameters = array("Operation" => "ItemLookup",
"ItemId" => $search,
"SearchIndex" => $category,
"IdType" => "UPC",
"ResponseGroup" => "Medium");
break;
case "TITLE" :
$parameters = array("Operation" => "ItemSearch",
"Title" => $search,
"SearchIndex" => $category,
"ResponseGroup" => "Medium");
break;
}
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
public function getItemByUpc($upc_code, $product_type)
{
$parameters = array("Operation" => "ItemLookup",
"ItemId" => $upc_code,
"SearchIndex" => $product_type,
"IdType" => "UPC",
"ResponseGroup" => "Medium");
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
public function getItemByAsin($asin_code)
{
$parameters = array("Operation" => "ItemLookup",
"ItemId" => $asin_code,
"ResponseGroup" => "Medium");
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
public function getItemByKeyword($keyword, $product_type)
{
$parameters = array("Operation" => "ItemSearch",
"Keywords" => $keyword,
"SearchIndex" => $product_type);
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
}
?>
Amazon signed request
The above class uses the ‘aws_signed_request’ function to generate the new request signature. Original code is by Ulrich Mierendorff, modified here to use cURL.
$value)
{
$param = str_replace("%7E", "~", rawurlencode($param));
$value = str_replace("%7E", "~", rawurlencode($value));
$canonicalized_query[] = $param."=".$value;
}
$canonicalized_query = implode("&", $canonicalized_query);
$string_to_sign = $method."\n".$host."\n".$uri."\n".
$canonicalized_query;
/* calculate the signature using HMAC, SHA256 and base64-encoding */
$signature = base64_encode(hash_hmac("sha256",
$string_to_sign, $private_key, True));
/* encode the signature for the request */
$signature = str_replace("%7E", "~", rawurlencode($signature));
/* create request */
$request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
/* I prefer using CURL */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$xml_response = curl_exec($ch);
if ($xml_response === False)
{
return False;
}
else
{
/* parse XML and return a SimpleXML object, if you would
rather like raw xml then just return the $xml_response.
*/
$parsed_xml = @simplexml_load_string($xml_response);
return ($parsed_xml === False) ? False : $parsed_xml;
}
}
?>
Which variabel must be change to search for ALL category? I try add const ALL = “All”; in api class but error : Invalid xml response.
How i can search for all category?
Thanks
Not work, there’s error like this :
Notice: Use of undefined constant B0067HQL30 – assumed ‘B0067HQL30’ in C:\xampp\htdocs\web20\index.php on line 8
Invalid xml response.
Kalkulator Hitung Angsuran KPR
Very Good Post and the the examples are added so helpful. here i went to share an other good post related to Amazon MWS It also be beneficial for.
http://gatelogix.com/blog/developement/amazon-marketplace-web-service/121
Great service and works fine even in 2014 🙂
However, and as many others stated over the years in the comments – How exactly do you access the returned array?
$request->items->item is only a single item, However for some reason $request->items->item[3] is the 3rd item,
It’s like ` $request->items->item ` is an array, but it points to the first element. Any thoughts?
When I ran the ‘Exanple.php’ I received an xml file output. What do I do with it. When unitTest\AmazonApiTest.php was run I got blank page and I could not find Framework.php as it says on the file ‘require_once ‘PHPUnit/Framework.php’
How do I implement it on wordpress? Thanks!
@Patrick, $request is an SimpleXMLElement Object, so you will need to iterate like the following example:
foreach($request->Items->Item as $i)
{
$d = $i->Offers->Offer->OfferListing->PercentageSaved;
echo $k++ . ” ” . $d . “\n”;
}
How to get availability status of an item whether available or not as field is not there regarding that..Please help…
You will need to add a ‘Availability’ parameter.
Check this link:
http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html
and also this:
http://docs.aws.amazon.com/AWSECommerceService/latest/DG/AvailabilityParameter.html
It’s working fine. How to convert this php xml to json?
use the following:
$result = json_encode($result);
Thanks man for help.how can we get next 10 products subsequently with script one by one after first 10 products..I modified the code a bit but got an error after that.Can you please help regarding that?
You will need to add the ‘ItemPage ‘ parameter in the ‘searchProducts’ function. Check the following link for additional details regarding the parameter.
http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html
I added that like below
case “TITLE” : $parameters = array(“Operation” => “ItemSearch”,
“Title” => $search,
“SearchIndex” => $category,
“ItemPage”=>10,
“ResponseGroup” => “Medium”);
break;
secondly i use like this
for ($i=1;$i “ItemSearch”,
“Title” => $search,
“SearchIndex” => $category,
“ItemPage”=>$i,
“ResponseGroup” => “Medium”);
// }
both throws error
when i give value as 10,it works but when i give more than 10,it throws me an error while returning data in xml format.How can i get a looping products with 10 products one by one..
ItemPage cannot be greater than 10. Each page returns 10 products for a total of 100 products. ItemPage can be only in the range 1-10.
So if some catalogue has products in number of lakhs. Then is there any way to retrieve them sir?
Thats fine.but how can we get rest of products if the list is quite big lets say we have lakhs of products for one category.how can we access that then sir?
You will have to “drill down” using browse nodes to get other products. Any single category will not have lakhs of products, but will be further sub-categorised using browse nodes.
http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/
http://docs.aws.amazon.com/AWSECommerceService/latest/DG/UnderstandingBrowseNodeResultsWhenDrillingDown.html
Thanks man for helpful links.I have one small query lets say I searched for women in title in category apparel,so I got some results out of which only 10 products were displayed.
So we need to try using various words like shoes,men ,casual etc. like I used women in my search to search through apparel category!!.May be we will get same products for various searches then we can insert it by comparing with existing asin numbers.
Is my approach correct?
I am using search keywords at the place where you are using x-men-origins in your example.
There is no right approach, but yours will work if you are using keyword for search. A better approach will be to use BrowseNodes to go to a subcategory. Also note that there is a limit to how much queries you can send to Amazon per hour. Please check the latest limit from their documentation.
K.thanks man for info
OMG..Just now i got access key and secret key..how can i use these details to work with amazon api..i am completely new to this..Thanks in advance.
How to get item amazon price that is selling (from amazon)?
I was using this code below, but I see now it show “More Buying Choices”, not amazon price.
Items->Item->OfferSummary->LowestNewPrice->FormattedPrice
I tried to find it in php array but it doesn’t exist.
I guess I have to use new function instead of getItemByAsin.
Hi Sameer, Thank you for the well written code.
I am able to successfully run searchProducts() and getItemByKeyword() functions. But the other two functions (i.e. getItemByUpc() and getItemByAsin()) are returning invalid XML error message.
I have tried tweaking these two functions quite a bit in order to make them work. But to no avail. Both these functions are pretty simple, but still, I am missing something. Could you please confirm if they return good XML?
Thanks once again!
Cannot really comment without knowing what the error message. In the ‘aws_signed_request.php’ file echo the $xml_response variable to see what error is returned.
My comment is still “Your comment is awaiting moderation.”
homeland, will have to check, currently ‘m out on leave.
Hello Sameer,
Thank You! This article was a great help for me. All works fine!
Chris
Could not connect to Amazon. Public_key, private_key and Associate Tag are filled but still get this error. Any solution for this ?
we are using above code and getting an error “Invalid xml response”.
what i felt my URL is not right
ecs.amazonaws.com /onca/xml AWSAccessKeyId=XXXXXXXXXXXXXX&AssociateTag=XXXXXXXXXXX-XX&Operation=ItemSearch&ResponseGroup=Medium&SearchIndex=DVD&Service=AWSECommerceService&Timestamp=2015-04-25T21%3A43%3A30Z&Title=X-Men%20Origins&Version=2009-03-31
please help me
thanks
Great – It works for me. Thank you so much
CAN ANYONE SUGGEST ME BEST THEME TO START AMAZON AFFILIATE USING ADVERTISING API .