2013年6月12日 星期三

Solved! How to Set Up Started With Amazon Product Advertising API

Solved! How to Set Up Started With Amazon Product Advertising API

Hi, here is Andy, and i am going to write this blog to tell you how to connect with Amazon Advertising API and get some output from the response XML.
If you are reading the Amazon API manual https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html, i am quite sure that you can not make your own PHP program to connect to the Amazon API and retrieve the variable successfully. Fortunately, I found that one guy in the internet who contribute a very very useful script to get value from the Amazon Advertising API, and we can edit from it.
You can get the source code to Access the Amazon Advertising API here.

What is the Product Advertising API

Amazon Product Advertising API is basically an API offered by Amazon for accessing data on every product that are being displayed at any Amazon site.

Create an Access Key

Just like most of the API that are offered by sites like Facebook, Twitter, Tumblr we need to create an access key to be used by our application. The access key that were going to create can be used to authenticate requests to any Amazon Web Service API and that includes the Product Advertising API.
In order to obtain an access key first you have to sign up for an amazon web service account at: aws.amazon.com. Youre amazon web service account is different from your amazon account that you use for shopping so you have to create a new account if you don’t already have one. Along the sign up process you will be asked for your credit card number. If you don’t have a credit card fret not because you can just close the tab when you reach that point and then access thesecurity credentials page where you can create a new access key. All you have to do once you reach that page is to scroll down a bit so you will see the Access Credentials section which basically shows you a table of the access keys that are associated with your account. Just click on the “Create new access key” link to create a new access key. Once that’s done you can just copy and paste the access key ID and the Secret Access key to the PHP file where you’re going to include the class for accessing the api later on.
amazon access keyamazon access key
You can also sign up for an amazon affiliate account: Amazon affiliate program so that you will have your own affiliate ID which you can also use in the class for accessing the API.

API Basics

There are only 3 things that you have to keep in mind while working with the Amazon Product Advertising API:
  • Operations
  • Response Groups
  • Browse Nodes

Operations

Operations are like methods in a class. This tells the API what specific action to do. Some of the most commonly used operations are:
  • ItemSearch – commonly used for returning one or more items that satisfies the parameters specified
  • ItemLookup – commonly used for returning a single item but can also return more items if the ASIN’s (Amazon Standard Identification Number) are separated by comma.
  • SimilarityLookup – used for returning items that are similar to the ASIN that is specified in the request.

Response Groups

Response groups are pre-defined result sets in the API. They are used for specifying the data that is to be returned by the API. Some of the most commonly used response groups are:
  • ItemAttributes
  • EditorialReview
  • Images
  • Medium
  • NewReleases
  • Reviews
  • ItemIds
You can check out the full list of available response groups here: Response Groups
Looking at the documentation provided by Amazon I don’t think there’s a way to specify the specific fields that you want to return so the best thing to do when working with the API is to select a response group that suits your needs. Medium and Large response groups isn’t really the best response group to use if you only want to get the items ASIN, title and selling price. So its wiser to check out what specific data are being returned by each response group before picking something like the Medium response group which returns a whole bunch of data that you might not need in the app that you’re trying to build. Choosing the right response group will have a considerable effect on performance as you’re only getting what’s actually needed.

Browse Nodes

Browse Nodes in simple terms are product categories in amazon. If you have already tried shopping for a product at amazon you might have notice that there’s a general category that you select when searching for a product like Electronics, Aparrel, Books, etc. These are the major browse nodes. As you go inside that category you will then see some sub-categories. For example you’re browsing through the Books category. In the right side you will see sub-categories like arts & philosophy, business & investing, children’s books, etc. All of these are represented by node Id’s. You can find the full list of some of the major nodes in Amazon here: Browse Node IDs
As you might have noticed in the table in the link above, every amazon site has different sets of major nodes since not every site has the same items available.
____________________________________
First Step: Connect to API and get the SimpleXMLElement Object
1. edit the amazon_api_class.php and insert your
private $public_key     = “AKIxxxxxxxxxx”;
private $private_key    = “NsECHcEu7J/a5DMk8ccSxxxxxxxxxxxxxxx”;
private $associate_tag  = “xxxxxxx-20″;
the above sample is dummy, insert your own API access key.
2. upload the aws_signed_request.php and Example.php into the folder with the amazon_api_class.php
The aws_signed_request.php is a php file to create Amazon Signature to access the data from API, please do not edit it.
while the Example.php and the php to call the function. it is easy to modify.
I have upload the script as the demo of Amazon Advertising API
http://www.iammakingmoney.com/demo/amazonapi/Example.php
You will see a plenty of “SimpleXMLElement Object” and the real result of Harry Plotter Video Game is shown at the bottom of the page.
 Great! we have already get the data successfully!
Sales Rank : 8829
Feature point 2 : Become a master in stealth – Use the Invisibility Cloak, Polyjuice Potion and Peruvian Instant Darkness Powder to avoid detection and sneak past adversaries.
Search KEY : harry
____________________________________
Step 2, get the Amazon Advertising API data as we want
1. Please confirm that you have done the previous step and load the information from API.
2. Now, we are going to get the specific data from the Amazon Advertisting API.  Please open the Example.php
See here
echo “Sales Rank : {$result->Items->Item->SalesRank}
”;
This code is calling “SaleRank” valuable from the response result.
But the common question is:
How to know the PATH to retrieve the specific valuable such as URL, ASIN and Editorial Review, and description?
the secret is, just Right Click mouse and view page source code, you will see the XML listed in a tidy manner!
For example if you want to get the value of  [totalpage] of the search result, the amazon advertising API code would be:
echo “Total Page : {$result->Items->totalpage}
”;
it is just TWO level under the $result, ITEMS and TOTALPAGE
One more example
How to get the ASIN ?
echo “ASIN : {$result->Items->Item->ASIN}
”;

One more extra!
How to get the Bulletin Point , Feature Point number two?
It is something about ARRAY, take this:
    echo “Feature point 2 : {$result->Items->Item->ItemAttributes->Feature[2]}
”;
Response Group of Amazon Advertising API
Right Click on the browser and view page source, you will see a tidy loop of XML

沒有留言:

張貼留言