Script to Scraping Amazon Product in PHP with Amazon API

Well, this time I will share about how to scraping amazon products using Amazon API. Many new amazon players are still confused how amazon API worked to scraping amazon product. I don`t suggest you to do the screen scraping, Because is not permissable. Scrap amazon products using the API is a legal way. Beside legal, its more easy to use than screen scraping.

Who still confused about scraping amazon products using APIs can see the video snippet below

 To build Scraping Amazon script, you need to prepare. 

1. Get your amazon public key and amazon private key from aws.amazon. Read here for more info.

 

scraping amazon product - public key and private key

2. Build Function in PHP to get Product Advertising API URL

I got the function from this site, visit there if you need to learn more. I do a little modification to be easy to use.

function aws_signed_request($region, $params, $public_key, $private_key)
{
$method = "GET";
$host = "ecs.amazonaws.".$region;
$uri = "/onca/xml";
$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z",time()); //may not be more than 15 minutes out of date!
$params["Version"] = "2009-03-31";
ksort($params);
$canonicalized_query = array();
foreach ($params as $param=>$value)
{
$param = str_replace("%7E", "~", rawurlencode($param));
$value = str_replace("%7E", "~", rawurlencode($value));
$canonicalized_query[] = $param."=".$value;
}
$canonicalized_query = implode("&", $canonicalized_query);
$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
$signature = rawurlencode($signature);
$request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
return $request;
}

Call that function with this syntax below

$parameters=array(
'Operation' =>'ItemLookup' ,
'ItemId' =>$sku , // change $sku with ASIN code
'AssociateTag' =>$tag, //Your AssociateTag url
'ResponseGroup' =>'Large' , //Small, Medium, Large or SellerListing
);
$public_key =$pub_key; // Change with your public key
$private_key=$priv_key; // Change with your private key
$ext='com'; //extension of server, see data.php
ksort($parameters); // To shorting variable $parameters
$A=aws_signed_request($ext,$parameters,$pub_key,$priv_key); // Call aws_signed_request

The results of the $A variable is you will get Product Advertising API URL like 

Script to Scraping Amazon Product in PHP with Amazon API

 If you get results as above, you’ve managed to get Amazon product XML. With a few additions and looping statements in the script above you will get thousands of Amazon`s data , within a matter of hours.

You can combine with my ASIN GRABBER SCRIPT to get a reliable program. See and try my example here.

For scraping amazon xml data and how to arrange the data so that look elegant, i will discuss on the next article, if you have a question please comment below 🙂 .

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

2 Comments

 Add your comment
  1. Thanks for this article. I am still stumped on how to get the public/private keys. I ‘thot’ I had them, but they appear to be certificate signature files (20 lines), not a normal 20-40 character string.
    I got them here:
    https://console.aws.amazon.com/iam/home?region=us-west-2#security_credential

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: