====== 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

${Title}

, which you would then search/replace with the relevant data, much neater than echoing like this. ==== Esempio di chiamata e di risposta ==== Chiamata HTML: Prova AWS
Risposta del Web Service:
********************************* 0.0932650000000000 True New Ship Christmas Eve and Other Stories Amazon Medium Images Music 11 2 B000002JX6 http://www.amazon.com/Christmas-Other-Stories-Trans-Siberian-Orchestra/dp/B000002JX6%3FSubscriptionId%3D1GNS2SV631922MHC65G2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB000002JX6 1183 http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL75_.jpg 75 75 http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL160_.jpg 160 160 http://ecx.images-amazon.com/images/I/61YS4JR02TL.jpg 500 500 http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL30_.jpg 30 30 http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL75_.jpg 75 75 http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL160_.jpg 160 160 http://ecx.images-amazon.com/images/I/61YS4JR02TL.jpg 500 500 Trans-Siberian Orchestra Audio CD 0075679273628 1598 USD $15.98 © 1996 Atlantic Recording Corp. / Lava Records 92736 1 40 560 25 490 Music © 1996 Atlantic Recording Corp. / Lava Records 1996-10-15 © 1996 Atlantic Recording Corp. / Lava Records Christmas Eve and Other Stories 075679273628 495 USD $4.95 147 USD $1.47 59 53 0 0 Amazon.com Is the Trans-Siberian Orchestra's <I>Christmas Eve and Other Stories</I> a holiday rock opera? Or perhaps just a holiday prog-rock disc? Or maybe it's New Age? Whatever the case may be, this isn't your typical Christmas album. Filled with electric guitar solos, plenty of synthesized keyboards, a children's choir, and lively drumming, <I>Christmas Eve</I> can only be compared to one other record, the Trans-Siberian Orchestra's <I>other</I> holiday disc, <I>The Christmas Attic</I>. On this CD, angelic vocal solos (on numbers such as "The Prince of Peace") are interspersed with driving instrumentals. Sentimental, occasionally bombastic, but as high-concept as holiday albums can be. <I>--Jason Verlinde</I> B00005T7NZ http://www.amazon.com/Christmas-Other-Stories-Trans-Siberian-Orchestra/dp/B00005T7NZ%3FSubscriptionId%3D1GNS2SV631922MHC65G2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00005T7NZ 282910 http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL75_.jpg 75 75 http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL160_.jpg 160 160 http://ecx.images-amazon.com/images/I/41kFGX1jQCL.jpg 300 300 http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL30_.jpg 30 30 http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL75_.jpg 75 75 http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL160_.jpg 160 160 http://ecx.images-amazon.com/images/I/41kFGX1jQCL.jpg 300 300 Trans-Siberian Orchestra Audio CD 4001617728925 Import 2449 USD $24.49 2 94 535 44 496 Music 2008-08-19 Christmas Eve and Other Stories 1667 USD $16.67 1753 USD $17.53 8 5 0 0 Amazon.com Is the Trans-Siberian Orchestra's <I>Christmas Eve and Other Stories</I> a holiday rock opera? Or perhaps just a holiday prog-rock disc? Or maybe it's New Age? Whatever the case may be, this isn't your typical Christmas album. Filled with electric guitar solos, plenty of synthesized keyboards, a children's choir, and lively drumming, <I>Christmas Eve</I> can only be compared to one other record, the Trans-Siberian Orchestra's <I>other</I> holiday disc, <I>The Christmas Attic</I>. On this CD, angelic vocal solos (on numbers such as "The Prince of Peace") are interspersed with driving instrumentals. Sentimental, occasionally bombastic, but as high-concept as holiday albums can be. <I>--Jason Verlinde</I> ==== Links ==== * [[http://forums.digitalpoint.com/showthread.php?t=1112957| Articolo Originale]] * [[https://aws-portal.amazon.com/gp/aws/developer/registration/index.html/| Registrazione per avere il KeyId]]