August 05

amazon product advertising api logo

The Amazon Product Advertising API (part of Amazon Web Services (AWS)) provides access to Amazon's product data. There are basically using SOAP and REST are two ways to access the AWS. For the Amazon Product Advertising API since

08/15/2009 to change the authentication method. Reason enough to the changed access in a small sample to look at more closely.
Used to extend an API Key is now a public and a private access key is necessary that one during the registration created. For SOAP requests, alternatively, the authentication via X.509 certificates is possible. An overview of the so called "Request identifier" is here .

In the example, the class AmazonService accept the request to the Amazon Product Advertising API. Consider only the case if all goes well. Must be added in productive use, of course, the error handling.
The created when registering access key is stored in the class as constants. The variable we need for the xml response from Amazon.

  A
 2
 3
 4
 5
  Amazon's service class {
    '< Euer öffentlicher Schlüssel >' ; apikey const = '<Your public key>';
    '< Euer privater Schlüssel >' ; private_key const = '<your private key>';
    '< Eure Associates Tracking ID >' ; ASSOCIATE_TAG const = '<Your Associates Tracking ID>';
    ; return $ xml; 

The main request is for the construction of the URL. In your request, and the parameters of the created with the private signature key access to Amazon to be transferred. This task is performed by the function buildURL .

  A
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
  $params private function BuildUrl ($ params
       = 'ecs.amazonaws.de' , $path = '/onca/xml' ) { $ Host = 'ecs.amazonaws.de', $ path = '/ onca / xml') { 

    "GET \n $host \n $path \n " ; $ Head = "GET \ n $ host \ n $ path \ n"; 

    / / Filter spaces and
    / / Add the time stamp and Associates Tracking ID
    / / On the parameters
    array_filter ( $params $ Params = array_filter ($ params
       ( '$x' , 'return $x;' ) ) ; , Create_function ('$ x', '$ return x;')); 
    'Timestamp' ] = gmdate ( 'Ymd\TH:i:s\Z' ) ; $ Params ['timestamp'] = gmdate ('Ymd \ TH: i: s \ Z');
    'AssociateTag' ] = self :: ASSOCIATE_TAG ; $ Params ['AssociateTag'] = self :: ASSOCIATE_TAG; 
    $params ) ; ksort ($ params); 

    implode ( '&' $ Body = implode ('&'
       ( create_function ( '$x,$y' , 'return "$x=".urlencode($y);' ) , Array_map (create_function ('$ x, $ y', '. Return "$ x =" urlencode ($ y);')
       ( $params ) , $params ) ) ; , Array_keys ($ params), $ params)); 

    / / Create the signature using the private key access
    urlencode ( base64_encode ( hash_hmac ( 'sha256' $ Sig = urlencode (base64_encode (hash_hmac ('sha256'
       . $body , self :: PRIVATE_KEY , true ) ) ) ; , $ Head $ body, self :: private_key, true))).; 

    ? $body &Signature= $sig " ; return? "http:// $ host $ path $ body = $ & Signature sig";
 } 

Thus prepared, we can now make out data for a book on Amazon query. The necessary code is in the function getBookByIsdn( $isbn ) encapsulated. To save the query results, the class Book used at this point I do not consider further, as they only methods to query and set variables (getter and setter) includes. An associative array, it would do at this point as well.

  A
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
  $isbn ) { function getBookByIsdn ($ isbn) {
    curl_init ( ) ; $ Ch = curl_init ();

    / Compile / The URL of the AWS-call
    array ( "Service" => "AWSECommerceService" $ Params = array ("Service" => "AWSECommerceService"
       => self :: APIKEY "Awsaccesskeyid" => self :: apikey
       => "ItemLookup" , "Operation" => "ItemLookup"
       => "ISBN" "IdType" => "ISBN"
       => $isbn "ItemId" isbn => $
       => "Medium" , "Response Group" => "medium"
       => "Books" ) ; , "Search index" => "Books");

    $this -> buildURL ( $params ) ; $ Url = $ this -> BuildUrl ($ params);

    / / Get data
    $ch , CURLOPT_URL , $url ) ; curl_setopt ($ ch, CURLOPT_URL, $ url);
    $ch , CURLOPT_HEADER , 0 ) ; curl_setopt ($ ch, CURLOPT_HEADER, 0);
    $ch , CURLOPT_RETURNTRANSFER , 1 ) ; curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);

    curl_exec ( $ch ) ; $ Result = curl_exec ($ ch);
    $ch ) ; curl_close ($ ch);   

    / / Read XML
    simplexml_load_string ( $result ) ; $ Xml = simplexml_load_string ($ result);
    xml = $xml ; $ This -> xml = $ xml;

    '/\d+/' preg_match ('/ \ d + /'
       -> Items -> Item -> ItemAttributes -> PublicationDate , $treffer ) ; $ Xml -> Items -> Item -> ItemAttributes -> Publication Date, $ matches);
    $treffer [ 0 ] ; $ Year = $ matches [0];

    / Store / preparation and of the result
    new Book ( ) ; $ Book = new Book ();
    setBarcode ( utf8_decode ( $ Book -> setBarcode (utf8_decode (
       Items -> Item -> ItemAttributes -> EAN ) ) ; $ Xml -> Items -> Item -> ItemAttributes -> UPC));
    setAuthor ( utf8_decode ( $ Book -> setAuthor (utf8_decode (
       Items -> Item -> ItemAttributes -> Author ) ) ; $ Xml -> Items -> Item -> ItemAttributes -> Author));      
    setJahr ( $jahr ) ; $ Book -> setJahr ($ year);
    setVerlag ( utf8_decode ( $ Book -> setVerlag (utf8_decode (
       Items -> Item -> ItemAttributes -> Publisher ) ) ; $ Xml -> Items -> Item -> ItemAttributes -> publisher));
    setTitel ( utf8_decode ( $ Book -> vitrectomy (utf8_decode (
       Items -> Item -> ItemAttributes -> Title ) ) ; $ Xml -> Items -> Item -> ItemAttributes -> Title));
    setInhalt ( utf8_decode ( $ Book -> Contents of the kit (utf8_decode (
       Items -> Item -> EditorialReviews -> EditorialReview -> Content ) ) ; $ Xml -> Items -> Item -> Editorial Reviews -> Editorial Review -> content));
    setBildUrl ( $xml -> Items -> Item -> MediumImage -> URL ) ; $ Book -> setBildUrl ($ xml -> Items -> Item -> Medium Image -> URL);

    ; return $ book;
 } 

After creating the URL for the request (line 13) and their means of calling curl_exec (line 20), the XML response in line 24 and then parsed into an object of the class Book is stored.

If only the call and the output.

  A
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
  <Php
    / / Query
    new AmazonService ( ) ; Amazon $ aws = new Service ();

    htmlspecialchars ( $_POST [ 'isbn' ] ) ; $ Isbn = htmlspecialchars ($ _POST ['isbn']);
    array ( $aws -> getBookByIsdn ( $isbn ) ) ; $ Books = array ($ aws -> getBookByIsdn ($ isbn));
 ?>
 <- Output ->
 <table>
    <tr>
       <th> No. </ th>
       <th> Cover </ th>
       <th> Title </ th>
       <th> Author (year) </ th>
       <th> content </ th>
       <th> Publisher </ th>
    </ Tr>
 <Php 
    $i = 0 ; $i < count ( $books ) ; $i ++ ) { for ($ i = 0; $ i <count ($ books); $ i + +) {
       $books [ $i ] ; $ Book = $ books [$ i];
 ?>
       <tr>
          + 1 ?> </td> <td> <? = $ i + 1?> </ td>
          -> getBildUrl ( ) ?> " /></td> <td> <img src = "<= $ book -?> getBildUrl ()>" /> </ td>
          -> getTitel ( ) ?> </td> <td> <= $ book -> getTitle ()> </ td>?
          -> getAuthor ( ) ?> ( <?= $book -> getJahr ( ) ?> )</td> <td> <= $ book -> getAuthor ()> (<= $ book -> getJahr ()>?) </ td>
          -> getInhalt ( ) ?> </td> <td> <= $ book -> getInhalt ()> </ td>?
          -> getVerlag ( ) ?> </td> <td> <= $ book -> getVerlag ()> </ td>?
       </ Tr>
 ?> <? Php}?>
 </ Table> 

Links:
Amazon Product Advertising API
Product Advertising API - Developer Guide
Registration
Amazon Web Services with PHP tutorial

Update:
Since 01/11/2011 is AssociateTag a parameter that is required for each request.

An alphanumeric token that uniquely Identifies to associate. This token is the means by Which Identifies the Amazon Associate to credit for a sale. If a request is made without identifying to associate, associate fees are not paid by Amazon. If the AssociateTag is included in the CartCreate request, the value is automatically included in AssociateTag for the PurchaseURL, Which is returned by CartCreate. To obtain associate at Day, go to https://affiliate-program.amazon.com . For more information, see Associate day.

Valid Values: An alphanumeric token distributed by Amazon that uniquely Identifies to associate. Use this value in all requests to receive credit for the customer's purchase.

gklinkmann written by \ \ tags: , , ,

9 comments to "Amazon Product Advertising API query with PHP"

  1. Andy Says:

    Hello,
    I am just by chance came across your blog because I stand right in front of this problem and it is very easy to understand not crack like me. Unfortunately, it is unclear to me how should I take the class book. Perhaps you could help me there.
    Yours sincerely,
    Andy

  2. gklinkmann Says:

    Sorry I am so late reporting. The Class Book includes only the data that is then called a Data Transfer Object (DTO) and has only the getter and setter methods for properties.

  3. Marco Says:

    Too bad there is unfortunately an AWS API script that is not incomplete or just does not work without problems. Amazon is unfortunately even less helpful.

  4. gklinkmann Says:

    @ Marco

    The only thing missing is the Book class, which is used only to store the data. These can be replaced by your own variables and write them within 5 minutes, because it consists only of Attibuten (eg title) and the get and set methods for these attributes.

    Since I have the Amazon class service as it is here even use, I would be grateful for detailed error descriptions.

  5. Mepo Says:

    Wow, heavy pieces without one of our previous knowledge. But I'm going to use your scripts again and try whether I can do it. Thank you first for the full list!

  6. Tilo Says:

    I see it like ... Mepo not be easy! Thank you for the great descriptions! I will also try my luck!

  7. Oliver Says:

    Hello, does anybody know if there is such a thing as a WordPress Plugin? So this brings me to hand the ASIN data or opinions of Amazon?

  8. gklinkmann Says:

    I am unfortunately none known.

  9. Mepo Says:

    Hi Oliver,

    Unfortunately not. I know a plugin that gets you on the basis of keywords and product days suitable products from Amazon and it digs into a DB. From there you can then further process them.

Add a comment

Yes, I would like to be notified about comments!