====== Accessing AWS using PHP to get Amazon product data - a short guide ======
==== Using SimpleXML ====
PHP5 by default offers the SimpleXML extension/library, whatever you want to call it, which takes most of the legwork out of parsing XML. So you don't necessarily need a special library for AWS, like someone asked, though that would be handy! SimpleXML mostly does it for you.
==== Accessing AWS ====
So what you do is set up the API call string as described in AWS documentation, I think you can probably get that bit, but this is an example (though cheesily programmed, there is a PHP command for elegantly forming URLs, but for the life of me I can't remember it right now!):
$APIcall = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&SearchIndex=Music&AWSAccessKeyId=[YOUR]&Operation=ItemSearch&AssociateTag=[YOURAFFILIATEID]&ResponseGroup=Large&Keywords=[ YOUR SEARCH ]";
$response = simplexml_load_file($APIcall);
The ItemSearch call set up as "$APIcall" would look up items in the "Music" SearchIndex specified using the keyword, and would return a "Large" data set in response. To do that you use the simplexml_load_file function, with the $APIcall url as your parameter.
**ResponseGroup** can be Small, Medium and Large.
==== Processing AWS results ====
As a result of this code, the variable $response becomes a SimpleXML object, something like an array, containing field names and values - no regexp required! (though you may need to convert data to strings or integers to use properly). If you echo this (actually, print_r), you get LOADS of data, and you can see just how much potential there is in AWS. In particular you get image links and product links already loaded with your affiliate ID which you submitted when making the call.
The data can be accessed like this in PHP, for example, you would cycle through each item as follows:
foreach ($response->Items->Item as $item)
{
$Title [] = $item->ItemAttributes->Title;
}
The foreach loop is used to cycle through all elements of an array, and it works on "objects" too - this SimpleXML data is an "object", remember. The "->" is sort of like the "." in JavaScript or VBA, denoting the hierarchy in the object. So you are setting up "Item" as your point of recursion. If you look at the XML, each Item has an ItemAttributes subset, and Title is a subordinate value to that. So this will build an array called $Title with all the titles in it!
This is quite simple, but you can see how by adding further loops (e.g. for each Item also recursing through the list of Reviews, say), you could build up a set of variables/arrays, with your relevant data. Now don't ask me how to parse the ChildNodes (as someone did), I haven't quite got my head round that because there is loads of deep recursion going on there, not sure how you handle that!
==== Displaying AWS data ====
Now, to display this basic data as HTML it's as simple as saying, e.g.:
foreach($Title as $CurrentTitle)
{
echo "
".$CurrentTitle."
";
}
Actually, you would write some sort of templating system where you would make a little HTML file containing tags like, say