Displaying Amazon lists on your blog using PHP

A common feature of blogs is the listing of albums or books that the author is currently enjoying. This article explains how to set up such a list in Amazon and show it on your blog using PHP and a REST web service.

If you’ve visited Amazon, you’ll have noticed that every time you view an item, it will be accompanied by a number of Lists, or ‘Listmania’ items. Each Listmania list has a title and a list of between 1 and 25 Amazon items.

A listmania that I’ve created can be found here. As you can see, I’m quite a fan of Pink Floyd, but you can obviously put whatever you want in your own list.

To create your list, visit the ‘Your Store’ tab and login. Once you’re in, click on the ‘YOUR PROFILE’ link that appears below the ‘Your store’ tab (which has now changed to include your name.)

Clicking on the profile link

Once the page has loaded, scroll down to the Listmania! section, and click on the “Create a Listmania!” List link.

Creating a listmania list

Once the page loads, you will be presented with the opportunity to create your list. Provide a name, your qualifications (I don’t know why this is relevant), and a list of ISBN/ASINs, which uniquely identify the product (there’s more information about these on the page.)

Setting up the list

Once the list is set up, we’ll need to make a request to an Amazon web service to get the information to display on the site. The first thing to do is head over to the Amazon Web Services site and sign up a free Amazon web services account. Amazon requires that you do this because you’ll need a ‘developer token’ before you can request any information.

There’s a huge amount of documentation available on the Amazon Web Services site, but we’ll be focussing on calling the web service using REST. This basically means that we’ll be making a web request to a specific URI, and getting back some XML, which we’ll parse and display on the site.

There a large number of web services, but our particular area of interest for retrieving our Listmania information is the Amazon E-Commerce Service. We’ll be using the ‘ListLookupOperation’ to get our Listmania information. The excellent documentation specifies a number of parameters, and our URL will look something like this:

http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=<Access Key>&Operation=ListLookup&ListType=Listmania&ListId=<List ID>&ResponseGroup=ListFull,Images

The Operation parameter is set to ‘ListLookup’ and the ListType to ‘Listmania’. The ResponseGroup determines what information our XML response will contain; in this instance, we’ll get full list information with image data. The AWSAccessKeyId is the ID you received when you signed up for the web services access, and the ListId is the ID of your listmania list (R2LGMJ3BF2KY9B for the one I described earlier.)

Quite a lot of XML’s returned, but a fragment is shown below:

	<ListName>Currently listening to</ListName>
		<Comment>Final cut</Comment>
				<Height Units="pixels">75</Height>
				<Width Units="pixels">75</Width>
				<Height Units="pixels">160</Height>
				<Width Units="pixels">160</Width>
				<Height Units="pixels">455</Height>
				<Width Units="pixels">455</Width>
			<!-- more information removed for brevity -->
				<Title>The Final Cut: Remastered</Title>

Making the request for the XML using PHP

To request the XML, we’ll use CURL, the Client URL Library, available since PHP version 4.0.2. For our needs, the code is quite simple:

$listID = "R2LGMJ3BF2KY9B";

$url = "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=" . $key . "&Operation=ListLookup&ListType=Listmania&ListId=" . $listID . "&ResponseGroup=ListFull,Images";    	
// make request to amazon:
$c = curl_init( $url);
curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1);
$xmlResponse = curl_exec( $c);
curl_close( $c);

Initially, we define two variables to store our access key and the id of the list we want to access. The next line creates the URL for the REST web service, building up the various parts to include the access key and list id.

We use CURL in the initial call to curl_init, passing in the URL for the web service. The next line, where we call curl_setopt, is important, since it determines that the response from the URL in the curl_exec line will be assigned to a string, rather than being output directly. This is important, since we’re going to be parsing it. curl_exec will get the response and the final line, calling curl_close, closes the CURL session and frees the associated resources.

Parsing the XML

To parse the XML, we’ll use SimpleXML, which requires PHP5. Using Simple XML simplifies XML handling, and fits all our needs for this example.

The obvious questions when parsing some XML are what are the elements that we’ll be parsing, and what are we going to output to the response. Well, looking at the XML fragment shown above gives us some clues as for the XML structure, and for the response, I’ve decided to go for a definition list containing an album image and the album name. For CSS styling, I’ll give the definition list a class of “listmania”. With all this in mind, we get the following PHP code:

$xml = simplexml_load_string( $xmlResponse);

$listManiaHtml = "<dl class="listmania">";
$itemCount = 0;
foreach( $xml->Lists->List->ListItem as $listItem)

		$listManiaHtml .= "<dt>";
		$listManiaHtml .= $listItem->Item->ItemAttributes->Title;
		$listManiaHtml .= "</dt>";
		$listManiaHtml .= "<dd>";
		$listManiaHtml .=  "<a href="http://www.amazon.co.uk/gp/product/" . $listItem->Item->ASIN . "" title="" . $listItem->Item->ItemAttributes->Title . "">";
		$listManiaHtml .= "<img src="" . $listItem->Item->SmallImage->URL . "" alt="" . $listItem->Item->ItemAttributes->Title . "" />";
		$listManiaHtml .= "</a>";
		$listManiaHtml .= "</dd>";

$listManiaHtml .= "</dl>";

To begin, the XML response from the REST web service is loaded using simple_xml_load_string function. I define a variable to store the listmania output, and begin by assigning the opening tag of the definition list.

Using simple XML’s traversal methods, I can associate XML elements in the response directly using a chained -> syntax. Each album is enclosed in a ListItem element, and this is referenced in the foreach loop. Within this loop, the ListItem element’s Title, ASIN (Amazon identifier), and small image URL are extracted and appended to the listmania output variable. The ASIN is used in a hyperlink to link directly to the album on the Amazon website.

Doing a simple echo on the resulting listmania variable is all that’s then required to output the semantic listmania. Shown below is what the output looks like:

<dl class="listmania">
    <dt>The Final Cut: Remastered</dt>
        <a href="http://www.amazon.co.uk/gp/product/B0001KZM3O" title="The Final Cut: Remastered">
            <img src="http://ecx.images-amazon.com/images/I/1163ZHTF2RL.jpg" alt="The Final Cut: Remastered" />
    <dt>Libertad (Special Edn)</dt>
        <a href="http://www.amazon.co.uk/gp/product/B000QEIMJG" title="Libertad (Special Edn)">
            <img src="http://ecx.images-amazon.com/images/I/11LmOg0O4pL.jpg" alt="Libertad (Special Edn)" />

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.