In this php tutorial I’ll show how to use Amazon API, get ASIN by url and get price of product using Amazon Advertising API with PHP.

At first sign up for Amazon Affiliate Program. Then you need to create AWS public and secret keys.

Open AWS website, click Account->Security Credentials. Login and you’ll see tab “Access Keys”. Create new key pair and store it somewhere.

Install Amazon Advertising API wrapper.

1
composer require exeu/apai-io

Create amazon.php

1
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
48
49
50
require_once 'vendor/autoload.php';
use ApaiIO\ApaiIO;
use ApaiIO\Configuration\GenericConfiguration;
use ApaiIO\Operations\Lookup;
/**
* Gets ASIN by url of a product
*/
function getASIN($url) {
$pattern = "%/([a-zA-Z0-9]{10})(?:[/?]|$)%";
preg_match($pattern, $url, $matches);
if($matches && isset($matches[1])) {
$asin = $matches[1];
} else {
echo "Couldn\'t parse url and extract ASIN: {$url}
\n";
return false;
}
return $asin;
}
$url = 'http://www.amazon.com/gp/product/1491910291/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1491910291&linkCode=as2&tag=achgu-20&linkId=A3CZKDVUDYL7PUFB';
$asin = getASIN($url);
echo "ASIN: $asin\n";
$conf = new GenericConfiguration();
try {
$conf->setCountry('com')
->setAccessKey('YOUR ACCESS KEY')
->setSecretKey('YOUR SECRET KEY')
->setAssociateTag('YOUR ASSOCIATE TAG');
} catch (\Exception $e) {
echo $e->getMessage();
}
$apaiIO = new ApaiIO($conf);
$lookup = new Lookup();
$lookup->setItemId($asin);
$lookup->setIdType('ASIN');
$lookup->setResponseGroup(['Offers']);
$conf->setResponseTransformer('\ApaiIO\ResponseTransformer\XmlToSimpleXmlObject');
$formattedResponse = $apaiIO->runOperation($lookup);
$price = $formattedResponse->Items->Item->OfferSummary->LowestNewPrice->Amount / 100;
echo "Price: $ $price\n";

There is a lot of other information in $formattedResponse as well. You can print it or check Amazon docs.