Indice
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.
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 "<h2>".$CurrentTitle."</h2>"; }
Actually, you would write some sort of templating system where you would make a little HTML file containing tags like, say <h2>${Title}</h2>, which you would then search/replace with the relevant data, much neater than echoing like this.
Esempio di chiamata e di risposta
Chiamata HTML:
<html> <head> <title>Prova AWS</title> </head> <body> <form method="get" action="http://webservices.amazon.com/onca/xml" name="AWS"> <input name="Service" value="AWSECommerceService" type="hidden"> <input name="AWSAccessKeyId" value="[ YOUR KEY ID ]" type="hidden"> <input name="Operation" value="ItemSearch" type="hidden"> <input name="SearchIndex" value="Music" type="hidden"> <input name="ResponseGroup" value="Medium,Images" type="hidden"> <input type="TEXT" name="Keywords" value="Christmas Eve and Other Stories" > <INPUT type="submit" name="mysubmit" value="Click!"> </form> </body> <html>
<!--
KeyID: 1GNS2SV631922MHC65G2
KeyID: AKIAIVHAR5JN3VX3YENQ
Account: Tom Riddle | Access Key ID: AKIAJPMLNV5IBJZN6AFA | Secret Access Key: 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
-->
Risposta del Web Service:
<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05"> <OperationRequest> <HTTPHeaders> <Header Name="UserAgent" Value="Mozilla/5.0"/> </HTTPHeaders> <RequestId>*********************************</RequestId> <Arguments> <Argument Name="Operation" Value="ItemSearch"/> <Argument Name="mysubmit" Value="Click!"/> <Argument Name="AWSAccessKeyId" Value="[ YOUR KEY HERE ]"/> <Argument Name="ResponseGroup" Value="Medium,Images"/> <Argument Name="SearchIndex" Value="Music"/> <Argument Name="Keywords" Value="Christmas Eve and Other Stories"/> <Argument Name="Service" Value="AWSECommerceService"/> </Arguments> <RequestProcessingTime>0.0932650000000000</RequestProcessingTime> </OperationRequest> <Items> <Request> <IsValid>True</IsValid> <ItemSearchRequest> <Condition>New</Condition> <DeliveryMethod>Ship</DeliveryMethod> <Keywords>Christmas Eve and Other Stories</Keywords> <MerchantId>Amazon</MerchantId> <ResponseGroup>Medium</ResponseGroup> <ResponseGroup>Images</ResponseGroup> <SearchIndex>Music</SearchIndex> </ItemSearchRequest> </Request> <TotalResults>11</TotalResults> <TotalPages>2</TotalPages> <Item> <ASIN>B000002JX6</ASIN> <DetailPageURL>http://www.amazon.com/Christmas-Other-Stories-Trans-Siberian-Orchestra/dp/B000002JX6%3FSubscriptionId%3D1GNS2SV631922MHC65G2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB000002JX6</DetailPageURL> <SalesRank>1183</SalesRank> <SmallImage> <URL>http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL75_.jpg</URL> <Height Units="pixels">75</Height> <Width Units="pixels">75</Width> </SmallImage> <MediumImage> <URL>http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL160_.jpg</URL> <Height Units="pixels">160</Height> <Width Units="pixels">160</Width> </MediumImage> <LargeImage> <URL>http://ecx.images-amazon.com/images/I/61YS4JR02TL.jpg</URL> <Height Units="pixels">500</Height> <Width Units="pixels">500</Width> </LargeImage> <ImageSets> <ImageSet Category="primary"> <SwatchImage> <URL>http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL30_.jpg</URL> <Height Units="pixels">30</Height> <Width Units="pixels">30</Width> </SwatchImage> <SmallImage> <URL>http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL75_.jpg</URL> <Height Units="pixels">75</Height> <Width Units="pixels">75</Width> </SmallImage> <MediumImage> <URL>http://ecx.images-amazon.com/images/I/61YS4JR02TL._SL160_.jpg</URL> <Height Units="pixels">160</Height> <Width Units="pixels">160</Width> </MediumImage> <LargeImage> <URL>http://ecx.images-amazon.com/images/I/61YS4JR02TL.jpg</URL> <Height Units="pixels">500</Height> <Width Units="pixels">500</Width> </LargeImage> </ImageSet> </ImageSets> <ItemAttributes> <Artist>Trans-Siberian Orchestra</Artist> <Binding>Audio CD</Binding> <EAN>0075679273628</EAN> <Label>© 1996 Atlantic Recording Corp. / Lava Records</Label> <ListPrice> <Amount>1598</Amount> <CurrencyCode>USD</CurrencyCode> <FormattedPrice>$15.98</FormattedPrice> </ListPrice> <Manufacturer>© 1996 Atlantic Recording Corp. / Lava Records</Manufacturer> <MPN>92736</MPN> <NumberOfDiscs>1</NumberOfDiscs> <PackageDimensions> <Height Units="hundredths-inches">40</Height> <Length Units="hundredths-inches">560</Length> <Weight Units="hundredths-pounds">25</Weight> <Width Units="hundredths-inches">490</Width> </PackageDimensions> <ProductGroup>Music</ProductGroup> <Publisher>© 1996 Atlantic Recording Corp. / Lava Records</Publisher> <ReleaseDate>1996-10-15</ReleaseDate> <Studio>© 1996 Atlantic Recording Corp. / Lava Records</Studio> <Title>Christmas Eve and Other Stories</Title> <UPC>075679273628</UPC> </ItemAttributes> <OfferSummary> <LowestNewPrice> <Amount>495</Amount> <CurrencyCode>USD</CurrencyCode> <FormattedPrice>$4.95</FormattedPrice> </LowestNewPrice> <LowestUsedPrice> <Amount>147</Amount> <CurrencyCode>USD</CurrencyCode> <FormattedPrice>$1.47</FormattedPrice> </LowestUsedPrice> <TotalNew>59</TotalNew> <TotalUsed>53</TotalUsed> <TotalCollectible>0</TotalCollectible> <TotalRefurbished>0</TotalRefurbished> </OfferSummary> <EditorialReviews> <EditorialReview> <Source>Amazon.com</Source> <Content>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></Content> </EditorialReview> </EditorialReviews> </Item> <Item> <ASIN>B00005T7NZ</ASIN> <DetailPageURL>http://www.amazon.com/Christmas-Other-Stories-Trans-Siberian-Orchestra/dp/B00005T7NZ%3FSubscriptionId%3D1GNS2SV631922MHC65G2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00005T7NZ</DetailPageURL> <SalesRank>282910</SalesRank> <SmallImage> <URL>http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL75_.jpg</URL> <Height Units="pixels">75</Height> <Width Units="pixels">75</Width> </SmallImage> <MediumImage> <URL>http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL160_.jpg</URL> <Height Units="pixels">160</Height> <Width Units="pixels">160</Width> </MediumImage> <LargeImage> <URL>http://ecx.images-amazon.com/images/I/41kFGX1jQCL.jpg</URL> <Height Units="pixels">300</Height> <Width Units="pixels">300</Width> </LargeImage> <ImageSets> <ImageSet Category="primary"> <SwatchImage> <URL>http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL30_.jpg</URL> <Height Units="pixels">30</Height> <Width Units="pixels">30</Width> </SwatchImage> <SmallImage> <URL>http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL75_.jpg</URL> <Height Units="pixels">75</Height> <Width Units="pixels">75</Width> </SmallImage> <MediumImage> <URL>http://ecx.images-amazon.com/images/I/41kFGX1jQCL._SL160_.jpg</URL> <Height Units="pixels">160</Height> <Width Units="pixels">160</Width> </MediumImage> <LargeImage> <URL>http://ecx.images-amazon.com/images/I/41kFGX1jQCL.jpg</URL> <Height Units="pixels">300</Height> <Width Units="pixels">300</Width> </LargeImage> </ImageSet> </ImageSets> <ItemAttributes> <Artist>Trans-Siberian Orchestra</Artist> <Binding>Audio CD</Binding> <EAN>4001617728925</EAN> <Format>Import</Format> <ListPrice> <Amount>2449</Amount> <CurrencyCode>USD</CurrencyCode> <FormattedPrice>$24.49</FormattedPrice> </ListPrice> <NumberOfDiscs>2</NumberOfDiscs> <PackageDimensions> <Height Units="hundredths-inches">94</Height> <Length Units="hundredths-inches">535</Length> <Weight Units="hundredths-pounds">44</Weight> <Width Units="hundredths-inches">496</Width> </PackageDimensions> <ProductGroup>Music</ProductGroup> <ReleaseDate>2008-08-19</ReleaseDate> <Title>Christmas Eve and Other Stories</Title> </ItemAttributes> <OfferSummary> <LowestNewPrice> <Amount>1667</Amount> <CurrencyCode>USD</CurrencyCode> <FormattedPrice>$16.67</FormattedPrice> </LowestNewPrice> <LowestUsedPrice> <Amount>1753</Amount> <CurrencyCode>USD</CurrencyCode> <FormattedPrice>$17.53</FormattedPrice> </LowestUsedPrice> <TotalNew>8</TotalNew> <TotalUsed>5</TotalUsed> <TotalCollectible>0</TotalCollectible> <TotalRefurbished>0</TotalRefurbished> </OfferSummary> <EditorialReviews> <EditorialReview> <Source>Amazon.com</Source> <Content>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></Content> </EditorialReview> </EditorialReviews> </Item> </ItemSearchResponse>

