Amazon Web Services using NuSOAP

Here’s a little article about connecting to amazon web services in PHP using NuSOAP.

Getting started

Firstly, get yourself over to NuSOAP and download a copy of NuSOAP. Scott Nichol is now the one who does most of the coding on this project appearance and he has a number of tutorials.

Then of course you’ll need to get hold of the amazon web service SDK and get a developer token. Its all free so no worries there.

Unpack NuSOAP to a directory, and create a new file called amazon.php. This file will be where we place our lovely code to get the details from amazon.

The Code – Keyword search

First we need to include the NuSOAP files:


Next we need to create out soap client and set some basic variables up:

$client = new soapclient('');
$namespace = '';
$action = '';

The namespace and action are the same in the case of amazon, but often they might be different.

There are two main types of searches I do, one is the keyword search, and the other is the browse node search. I’ll cover the keyword search first and then highlight the differences between the two.

For a keyword search we need to pass in an array of parameters, detailing what we want to search for, where, and what we want returned. Here is an example:

$params = array(
  'keyword' => 'the keyword', 
  'page' => '1',
  'mode' => 'the mode',
  'tag' => 'wackylabsnet-21',
  'type' => 'lite',
  'devtag' => 'XXXXXXXX',
  'locale' => 'uk'

keyword: The keyword value should be what you want to search for (e.g. ‘walks, lake district’ or ‘programming, .net’)
mode: This is where you should search in the amazon database, and example values are ‘books’, ‘dvds’, ‘music’. Regional variations exist for some modes, e.g. ‘books-uk’.
devtag: This is your developer tag
locale: Here I’m using the ‘uk’ locale, so prices etc. are returned in £ from the UK database. The default is ‘us’.
type: This is the xml-schema used to return the details. In most cases the ‘lite’ one will be enough, unless you also want reviews, comments and used item sale details as well.

Then we actually do the search:

$method = "KeywordSearchRequest";
$result = $client->call($method, 
  array('KeywordSearchRequest' => $params), 
  $namespace, $action);

The result is an array with many properties, but the one we are most interested in is the Details value, which itself is an array of the items returned.

foreach($result['Details'] as $detail)
  $price = $detail['OurPrice'];
  $name = $detail['ProductName'];
  $image = $detail['ImageUrlSmall'];

As you can see here I’m not doing anything with these values, but you can then output them, stored them in a database or whatever.

The Code – Browsing a Node

For browsing, the only difference is the method and the params array:

$params = array(
  'browse_node' => 'the node', 
  'page' => '1',
  'mode' => 'the mode',
  'tag' => 'wackylabsnet-21',
  'type' => 'lite',
  'devtag' => 'XXXXXXXX',
  'locale' => 'uk'

$method = "BrowseNodeSearchRequest";
$result = $client->call(
  array('BrowseNodeSearchRequest' => $params), 

Here browse_node will be the node number, taken from an amazon URL (look for “../browse/-/123456/..” in the URL, where 123456 is the node number.)


Well I hope that was easy enough. Of course there are a lot more details returned in the resulting array, especially if you choose ‘heavy’ as the type. Also, there are a number of other optional parameters you can pass in, such as sort order you want the results returned in. There are also other types of searches, such as artist, author, list mania or ever wishlist searches.

And finally, the new version of PHP, version 5, which has just been released has new built in support for SOAP, so eventually the NuSOAP package will become defunct to some extent.

Look out for a forthcoming article about how to create server site SOAP web services, including WSDL generation using NuSOAP.

[Listening to: The Chokin’ Kind – Joss Stone – The Soul Sessions (03:37)]

1 thought on “Amazon Web Services using NuSOAP”

Comments are closed.