Reputation: 420
I need to get Amazon product details(Item name, Brand, Price etc..) by searching ASIN.
I look at Amazon's API and not able to find the exact code to do it. https://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html
I want to integrate with PHP or Javascript. Any help? Thanks in Advance.
Upvotes: 0
Views: 2931
Reputation: 713
Here is a simple example. You have to replace $public_key
, $private_key
and $associate_tag
with your own values.i have not tested this but go through this and let us know whether its useful.
<?php
include('aws_signed_request.php');
$public_key = '********';
$private_key = '********';
$associate_tag = '********';
// generate signed URL
$request = aws_signed_request('com', array(
'Operation' => 'ItemLookup',
'ItemId' => 'B008GG93YE',
'ResponseGroup' => 'Small'), $public_key, $private_key, $associate_tag);
// do request (you could also use curl etc.)
$response = @file_get_contents($request);
if ($response === FALSE) {
echo "Request failed.\n";
} else {
// parse XML
$pxml = simplexml_load_string($response);
if ($pxml === FALSE) {
echo "Response could not be parsed.\n";
} else {
if (isset($pxml->Items->Item->ItemAttributes->Title)) {
echo $pxml->Items->Item->ItemAttributes->Title, "\n";
}
}
}
?>
aws_signed_request.php code
<?php
function aws_signed_request($region, $params, $public_key, $private_key, $associate_tag=NULL, $version='2011-08-01')
{
// some paramters
$method = 'GET';
$host = 'webservices.amazon.'.$region;
$uri = '/onca/xml';
// additional parameters
$params['Service'] = 'AWSECommerceService';
$params['AWSAccessKeyId'] = $public_key;
// GMT timestamp
$params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
// API version
$params['Version'] = $version;
if ($associate_tag !== NULL) {
$params['AssociateTag'] = $associate_tag;
}
// sort the parameters
ksort($params);
// create the canonicalized query
$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);
// create the string to sign
$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
// calculate HMAC with SHA256 and base64-encoding
$signature = base64_encode(hash_hmac('sha256', $string_to_sign, $private_key, TRUE));
// encode the signature for the request
$signature = str_replace('%7E', '~', rawurlencode($signature));
// create request
$request = 'http://'.$host.$uri.'?'.$canonicalized_query.'&Signature='.$signature;
return $request;
}
?>
Upvotes: 2