The following code example will show you how to query the amazon.com database and return products you requested with your affiliate code, some pictures, and editorial reviews. When finished you should be able to make a basic amazon request and return the unformatted result in a php page.
For an overview of what a REST api is see the video Rest Api In Plain English.
You need to provide your amazon developer key, associate tag, some keywords, and a search index to make a query. We do that with our first block of code:
When using amazon's rest api you provide them with a URL request and they will return an answer. Here is the url construction with our variables in the url:
We use CURL to connect to amazon and return their xml answer in a variable with the following code:
PHP5 has simplexml that really makes working with XML very easy. Simply load a text variable with xml data into an xml element and you can traverse the nodes all day long.
note: to see the notes available uncomment the $whaturl variable and copy and paste that into the browser to see the raw result amazon returns
Here's the code to load the amazon response into XML that PHP can understand:
Finally we want to loop through the XML data and print out the data we want to display to users. See the previous step's note on how to see all of the nodes returned by amazon. See this link on how to traverse xml nodes with php.
Here is the code all put together now:
take that url - echo $whaturl, and uncomment it and then copy and paste that into your browser to see exactly what amazon returns
this will not only interest you but will show you what other data is available to show off ;)
For an overview of what a REST api is see the video Rest Api In Plain English.
Set Some Basic Variables
You need to provide your amazon developer key, associate tag, some keywords, and a search index to make a query. We do that with our first block of code:
//change these variables to yours
$amazonid = ""; //your developer key
$amazontag="45n5-20";//your associates Id to get paid
$keywords="make money";//your keywords to search
$safequery=urlencode($keywords); //make the keywords url friendly
$searchindex="Blended"; //change to the index you want to search
Create The Url To Request The Info From Amazon
When using amazon's rest api you provide them with a URL request and they will return an answer. Here is the url construction with our variables in the url:
////now build the url like amazon wants it
$whaturl="http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService";
$whaturl.="&IdType=ASIN&ResponseGroup=Large&SearchIndex=" . $searchindex;
$whaturl.="&Operation=ItemSearch&Keywords=" . $safequery;
$whaturl.="&AWSAccessKeyId=" . $amazonid;
$whaturl.="&AssociateTag=" . $amazontag . "&Version=2007-05-14";
Connect To Amazon And Get The Result
We use CURL to connect to amazon and return their xml answer in a variable with the following code:
//let's connect to the amazon url with curl and grab the response
//we'll store the response in the $data variable
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $whaturl);
$data = curl_exec($ch);
curl_close($ch);
Convert the Amazon Response to XML that PHP can understand
PHP5 has simplexml that really makes working with XML very easy. Simply load a text variable with xml data into an xml element and you can traverse the nodes all day long.
note: to see the notes available uncomment the $whaturl variable and copy and paste that into the browser to see the raw result amazon returns
Here's the code to load the amazon response into XML that PHP can understand:
//now we will create a php element out of the data so
//we can then loop through and grab our results
$xml = new SimpleXMLElement($data);
Finally we want to loop through the XML data and print out the data we want to display to users. See the previous step's note on how to see all of the nodes returned by amazon. See this link on how to traverse xml nodes with php.
//now we loop through the xml document to grab what we want
if ($xml->Items->Item) {
$x=0;
foreach ($xml->Items->Item as $item) {
echo "<a href=\"http://www.amazon.com/exec/obidos/asin/" . $item->ASIN . "/$amazontag\"><img border=\"0\" src=\"" . $item->SmallImage->URL . "\" style=\"margin-right:10px;\" align=\"left\" alt=\"\" /></a>";
echo "<b>" . $item->ItemAttributes->Title . "</b><br /><br />";
echo "<a href=\"http://www.amazon.com/exec/obidos/asin/" . $item->ASIN . "/$amazontag\">View This Product at Amazon</a><br /><br />";
echo $item->EditorialReviews->EditorialReview->Content . "<br /><br />";
echo "<div style=\"clear:both\"></div>";
}}
All together now
Here is the code all put together now:
<?php
//change these variables to yours
$amazonid = ""; //your developer key
$amazontag="45n5-20";//your associates Id to get paid
$keywords="make money";//your keywords to search
$safequery=urlencode($keywords); //make the keywords url friendly
$searchindex="Blended"; //change to the index you want to search
////now build the url like amazon wants it
$whaturl="http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService";
$whaturl.="&IdType=ASIN&ResponseGroup=Large&SearchIndex=" . $searchindex;
$whaturl.="&Operation=ItemSearch&Keywords=" . $safequery;
$whaturl.="&AWSAccessKeyId=" . $amazonid;
$whaturl.="&AssociateTag=" . $amazontag . "&Version=2007-05-14";
//uncomment the next line to view the xml returned
//echo $whaturl . "<br />";
//let's connect to the amazon url with curl and grab the response
//we'll store the response in the $data variable
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $whaturl);
$data = curl_exec($ch);
curl_close($ch);
//now we will create a php element out of the data so
//we can then loop through and grab our results
$xml = new SimpleXMLElement($data);
//now we loop through the xml document to grab what we want
if ($xml->Items->Item) {
$x=0;
foreach ($xml->Items->Item as $item) {
echo "<a href=\"http://www.amazon.com/exec/obidos/asin/" . $item->ASIN . "/$amazontag\"><img border=\"0\" src=\"" . $item->SmallImage->URL . "\" style=\"margin-right:10px;\" align=\"left\" alt=\"\" /></a>";
echo "<b>" . $item->ItemAttributes->Title . "</b><br /><br />";
echo "<a href=\"http://www.amazon.com/exec/obidos/asin/" . $item->ASIN . "/$amazontag\">View This Product at Amazon</a><br /><br />";
echo $item->EditorialReviews->EditorialReview->Content . "<br /><br />";
echo "<div style=\"clear:both\"></div>";
}}
?>
45n5 (946) says on November 18, 2007:
thanks stu. take that url - echo $whaturl, and uncomment it and then copy and paste that into your browser to see exactly what amazon returns
this will not only interest you but will show you what other data is available to show off ;)
sarahG (34) says on November 21, 2007:
Nice post Mark. This is something I've been wanting to learn for years, even have a book on it (which is most likely out of date now!). Just never had the time. However I'll certainly find the time to play with your code :o)


