Filzhut.net Filzhut.net amazon_functions for PHP (Version 0.9)
Making the work with Amazon.com™ Web Services Version 1.0 much easier

Work with the search reply

The amazon_functions search functions have the advantage that they don't reply the searchresult as the XML-file amazon™ delivers, but as an array which is much easier to use. You can use this array for other amazon_functions like amazon_create_productlist or use it as shown below. In the returned array he structure of the XML is maintained (except of the first node called "ProductInfo" which is embezzled). Lets have a look at a usually XML result from a search at the XML-API:


<?xml version="1.0" encoding="UTF-8"?>
<ProductInfo>
   <Details url="http://www.amazon.com/exec/obidos/ASIN/039480001X">
      <ASIN>039480001X</ASIN>
      <ProductName>The Cat in the Hat</ProductName>
      <Catalog>Book</Catalog>
      <Authors>
          <Author>Seuss</Author>
          <Author>Theodor Seuss Geisel</Author>
          <Author>Dr. Seuss</Author>
      </Authors>
      <ReleaseDate>June 1957</ReleaseDate>
      <Manufacturer>Random House</Manufacturer>
      <ListPrice>$8.99</ListPrice>
      <OurPrice>$8.99</OurPrice>
      <UsedPrice>$6.95</UsedPrice>
      <ImageUrlSmall>http://images.amazon.com/images/P/039480001X.01.THUMBZZZ.jpg</ImageUrlSmall>
      <ImageUrlMedium>http://images.amazon.com/images/P/039480001X.01.MZZZZZZZ.jpg</ImageUrlMedium>
      <ImageUrlLarge>http://images.amazon.com/images/P/039480001X.01.LZZZZZZZ.jpg</ImageUrlLarge>
   </Details>
  </ProductInfo>

Our functions transform this data into the form of an array which would look like this:


Array
(
    [Details] => Array
        (
            [0] => Array
                (
                    [ASIN] => Array
                        (
                            [0] => 039480001X
                        )
                    [ProductName] => Array
                        (
                            [0] => The Cat in the Hat
                        )
                    [Catalog] => Array
                        (
                            [0] => Book
                        )
                    [Authors] => Array
                        (
                            [0] => Array
                                (
                                    [Author] => Array
                                        (
                                            [0] => Theodor Seuss Geisel
                                            [1] => Seuss
                                            [2] => Dr. Seuss
                                        )
                                )
                        )
                    [ReleaseDate] => Array
                        (
                            [0] => June 1957
                        )
                    [Manufacturer] => Array
                        (
                            [0] => Random House
                        )
                    [ListPrice] => Array
                        (
                            [0] => $8.99
                        )
                    [OurPrice] => Array
                        (
                            [0] => $8.99
                        )
                    [UsedPrice] => Array
                        (
                            [0] => $6.95
                        )
                    [ImageUrlSmall] => Array
                        (
                            [0] => http://images.amazon.com/images/P/039480001X.01.THUMBZZZ.jpg
                        )
                    [ImageUrlMedium] => Array
                        (
                            [0] => http://images.amazon.com/images/P/039480001X.01.MZZZZZZZ.jpg
                        )
                    [ImageUrlLarge] => Array
                        (
                            [0] => http://images.amazon.com/images/P/039480001X.01.LZZZZZZZ.jpg
                        )
                )
        )
)

In plain english: The search function gets the XML-string from amazon, embezzles the first node "ProductInfo", and pushs the XMLs' data into an array structure. The name of the nodes in the XML are also the keys of the array. The following example would printout the title of the replied book, which is "The Cat in the Hat" in this case, and the books' author.

Example 1. Disply the productname & author of the first book which was found:

$Bookdata=amazon_search_asin("039480001X");
$ProductName=$Bookdata[Details][0][ProductName][0];
$Author=$Bookdata[Details][0][Authors][0][Author][0];
echo "ProductName:  $ProductName<br>";
echo "Author: $Author";

If there wouldn't be just one book in the replied array, but two or more you just had to replace the "0" after "Details" with a "1" and you could printout the title and author of the second book the search replied.

Example 2. Disply the productname & author of the second book which was found:

$Bookdata=amazon_search_asin("039480001X");
$ProductName=$Bookdata[Details][1][ProductName][0];
$Author=$Bookdata[Details][1][Authors][0][Author][0];
echo "ProductName:  $ProductName<br>";
echo "Author: $Author";

Sometimes a book has more than one author or another field is existing twice. In this case you don't have to call the "0" behind this key, but the "1" or "2". So you'll get the second or third value of this array.

Example 3. Display the productname and the 2nd author field:

$Bookdata=amazon_search_asin("039480001X");
$ProductName=$Bookdata[Details][1][ProductName][0];
$Author=$Bookdata[Details][1][Authors][0][Author][1];
echo "ProductName:  $ProductName<br>";
echo "Author: $Author";


© by Filzhut.net, Daniel Filzhut 2002