PHP Amazon Product advertising API tutorial




4.54 K Views | Rishi Kashyap | 3 FOLLOW
    X

    PHP Amazon Product advertising API tutorial


    0

    In this PHP tutorial about Amazon Product advertising API we will try to find offers on Amazon website using Amazon product API and also learn how to get required Amazon product details. I read documentation about Amazon Product advertising API but was still unable to use it, until I found Accessing Amazon Product Advertising API by Sameer Borate. So here are simple steps to use Amazon Product advertising API with PHP

    1. Please Join Amazon Affiliate program and get your Associate Id
    2. Signup for Amazon Product advertising API
    3. Signup and get your AWSAccessKeyId and AWSSecretKey from Amazon Web service.
    4. Download amazon_api_class from codeigniter.
    5. Unzip "AmazonAPI" and open "amazon_api_class.php" and edit $public_key (line 42), $private_key (line 49) and $associate_tag (line 57) with your AWSAccessKeyId, AWSSecretKey and Associate Id respectively.
    6. Place the folder in your server root or local host and open "Example.php" in your browser. You should be getting the result using Amazon Product advertising API now !
    For me the Example worked perfectly but it was showing individual product details and so I tweaked a little, altered few things for Amazon India affiliate (com to in) and removed all "verifyXmlResponse()" check from the script as it was not showing returned Amazon data for certain "ResponseGroup".

    My need was to find offers in Amazon affiliate to avoid common Affiliate marketing problems. The below code first tries to find offers on Amazon India website in Electronic section for keywords "Mobile Phones". Of course the keyword and some other details can come from text field in HTML Form. The final code in PHP<?php
    include("amazon_api_class.php");

    //valid parameters for $region are 'com','co.jp','fr','cn','in','de','co.uk','ca','es','it','com.br'

    $region='in';
    $category='ELECTRONICS';
    $keyword='Mobile Phones';
    $product_type='Electronics';
        $obj = new AmazonProductAPI();
        
        try
        {
            //$result = $obj->searchProducts("X-Men Origins",AmazonProductAPI::DVD,"TITLE");
            $result = $obj->getItemByKeyword($keyword,$product_type);
        }
        catch(Exception $e)
        {echo $e->getMessage();}

    //print_r($result);exit;
    //For each item returned
    foreach($result->Items->Item as $tdata)
    {
      //Get ASIN details
    $asin=$tdata -> ASIN;
    //Number of offers
    $offercount=$tdata -> Offers ->MoreOffersUrl;
    //If no offers continue;
    if($offercount == '0'){continue;}
    //If offers then get Item details by ASIN
    $result1 = $obj->getItemByAsin($asin);
    //print_r($result1);exit;
    //For each item detail
    foreach($result1->Items->Item as $tdata1)
    {$pageurl=$tdata1 ->DetailPageURL;
    $medimg=$tdata1 ->MediumImage->URL;
    $title=$tdata1->ItemAttributes->Title;
    $features=$tdata1 ->ItemAttributes->Feature[0];
    $features .=$tdata1 ->ItemAttributes->Feature[1];
    $features .=$tdata1 ->ItemAttributes->Feature[3];
    $retailprice=$tdata1->ItemAttributes->ListPrice->FormattedPrice;
    $saleprice=$tdata1->OfferSummary->LowestNewPrice->FormattedPrice;
    //Show the details about product and offer
    echo "<br/><a href='$pageurl'><img src='$medimg'/><br/>
    <b>$title</b><br/>
    $features<br/>
    $retailprice<br/>
    $saleprice
    </a><br/>";
    }
    }
    ?>
    For details, please refer
    1. Basic Authentication Process
    2. Search Index support by locale
    3. Amazon Associates Web Service Quick reference cards (pdf)
    4. To understand the Authentication process API scratchpad or a downloadable javascript page Signed request helper
    I highly recommend to play around with Amazon Product advertising API and "Response group" for better understanding of Amazon product API. Best of luck.PHP Amazon Product advertising API tutorial

    Rishi Kashyap | 5 years ago | EDIT | REPLY
    • X
      0

      Hi,

      Using your sample code I am getting this error "Invalid xml response."

      Regards,
      Partha  

      Isolz Infotech | 5 years ago | EDIT | REPLY
    • X
      1

      I think Amazon Product advertising API does not have a robust xml encoder and hence for certain "ResponseGroup" it does not encode the html properly in the returned xml.
      I told before too that I removed all "verifyXmlResponse()" check from the amazon_api_class and parsed it separately.
      Please remove else
                  {
                      throw new Exception("Invalid xml response.");
                  }
      hopefully that should work for you. Best of luck.  

      Rishi Kashyap | 5 years ago | EDIT | REPLY
    • X
      0

      Thanks for your reply.

      I removed the exception line as you advice.

      Now I got this error:

      Could not connect to AmazonSales Rank :
      ASIN :

      I changed "com" to "in" in line 111 of amazon_api_class.api:

              private function queryAmazon($parameters)
              {
                  return aws_signed_request("in", $parameters, $this->public_key, $this->private_key, $this->associate_tag);
              }  

      Isolz Infotech | 5 years ago | EDIT | REPLY
    • X
      0

      Hi

      I wanted to know if we can implement the API in a way that we can display all the lightning deal products on our page. Anyone cracked it as of now, or know of how to do that?

      Thanks  

      Sandy Donor | 4 years ago | EDIT | REPLY



    YOU MAY ALSO LIKE
    tags  MAKE MONEY ONLINE PHP TUTORIAL