Table Of Contents

Introduction to the Amazon Affiliate API

Connecting to Amazon's API is a very powerful way display up-to-date data on various products your customers might be interested in. Amazon Affiliate Sites are some of the most profitable websites on the internet, far surpassing Google Adsense sites.

Amazon's Affiliate API is actually called the Product Advertising API, after being renamed from Amazon Associates Web Service. Using this API, we can lookup specific products on Amazon and obtain their information, such as live price, images, description and seller details.

This tutorial is going to show you, in the most simplest terms, how to lookup a product. We'll be using PHP's cURL library to access the API and extracting the returned data into an array for us to use.

Product Advertising Operations

We are going to be using Amazon's ItemLookup operation. Amazon has a few operations:

Operation

Description

ItemSearch

Search for items on Amazon

SimilarityLookup

Look for similar items to ones you've already searched for

ItemLookup

Lookup a specific item by amazon ID (ASIN) or ISBN.

Cart Operations

A bundle of different cart operations for adding items directly into a user's cart

How To Find an Amazon Items ASIN

Since we'll be using ItemLookup, we need to use either the item's amazon ASIN or ISBN (if it's a book) to lookup the item we are trying to find. You can obtain this information by looking up any item on amazon.com and searching for it on the item page. The ASIN is also found in the URL:

https://www.amazon.com/gp/product/B01IG0E1F0/ref=s9u_nwrsa_gw_i1?

The bolded part is the Amazon ASIN.

How to get Amazon Product Advertising API Keys

We have our item details, we know what operation we'll be using and we know the language we will be coding in (PHP!).

The last piece of the puzzle is to get our developer access keys. If you've ever used an API, you'll be familiar with this process.

Just head over to Amazon's Product Adversting API page and sign up to access it. You can use your Amazon account to do this quickly. Next, you'll need to register for a secret key and an access key. This can be done by going to the Manage Your Account section and clicking on the security credentials area.

Amazon Affiliate API PHP Tutorial

Now let's write some code.

For the purpose of this tutorial, I'll create a simple PHP Class that we'll use to make any calls to the API. We can then build on this class to add in more operations in the future.

Building the Amazon API URL

In order to use the ItemLookup function, we need to make a call to the Amazon API URL:

http://webservices.amazon.com/onca/xml?

This URL takes several variables that will determine what we lookup. We need to specify the following:

Variable

Example

Access Key

382hfh2hgfy3yy4y8385838528

Associates ID

Patchesoft-13

Operation

ItemLookup

Response Group

Small,Images,OfferSummary

Service

AWSECommerceService

Timestamp

2017-03-18 02:22:22

Version

2013-08-01

Some of the above variables we don't need to worry about at all, such as Service and Version. But let's take a look at some of the ones we don't know:

Operation - we mentioned the API has several operations you can use. We are using the ItemLookup operation here but you can use any of the listed ones in their API.

Response Group - This determines what data you wish to receive from your API request. In the above example, we use Small,Images,OfferSummary. Each Response Group can be separated by a comma. In this case Small means we want a basic summary of the items data, a list of all images associated with the product and a list of information about the item's pricing. List of Response Groups.

TimeStamp - We need to make sure the request we make has a valid timestamp in the required format. If you follow this guide, you'll be fine.

Taking all this information in, let's write our build_link function which will apply all these variables to our Amazon API URL.

We assign all our variables to the the variable $url_params. In some instances, we need to take care of URL formatting because Amazon requires the data to be properly formatted when sent to them. I.e. $this->response_groups = str_replace(",", "%2C", $this->response_groups);

We'll be assigning the $this->itemID in another function.

The generate_signature() function will be explained next.

But this takes care of most of the hard work we need for building our API request.

Amazon API Signature Request

Each API request we make requires a signature. This signature is made up of a hash sequence of data, including making use of your secret key you obtained earlier. The code for generating the signature is below:

This function uses cURL to send a request to the Amazon API URL we just constructed in build_url(). It then takes the response given back and loads it into an XML object using the native php simplexml_load_string().

We can now access the item data from Amazon using the $this->XML variable.

For example, to grab the item's name, we can do:

$itemname = $this->XML->Items->Item->ItemAttributes->Title;

Learning the XML structure can be a bit of a pain in the beginning, but using var_dump on the object will help you understand it better. I wrote a simple function to get the data I needed below:

Using the Amazon Affiliate API

You can now make your own affiliate script to obtain products from Amazon. There are some great Amazon Affiliate examples on the internet that you can take a look at to see how they advertise products. With the API toolset, you can be a lot more creative with your advertising marketing.

Existing Amazon Affiliate API PHP Libraries

There are a couple of useful pre-built libraries out there that already do this, but they are lot more complicated and feature-rich than what I needed. If you need something more substantial, take a look at these: