亚马逊通过使用 API PHP/jQuery 搜索 ASIN 获取商品详情
Amazon get item details by searching ASIN using API PHP/jQuery
我需要通过搜索 ASIN 获取亚马逊产品详细信息(商品名称、品牌、价格等)。
我查看了 Amazon 的 API,但无法找到执行此操作的确切代码。
https://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html
我想与 PHP 或 Javascript 集成。有什么帮助吗?提前致谢。
这是一个简单的例子。您必须将 $public_key
、$private_key
和 $associate_tag
替换为您自己的 values.i 尚未对此进行测试,但请仔细阅读并让我们知道它是否有用。
<?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代码
<?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;
}
?>
我需要通过搜索 ASIN 获取亚马逊产品详细信息(商品名称、品牌、价格等)。
我查看了 Amazon 的 API,但无法找到执行此操作的确切代码。 https://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html
我想与 PHP 或 Javascript 集成。有什么帮助吗?提前致谢。
这是一个简单的例子。您必须将 $public_key
、$private_key
和 $associate_tag
替换为您自己的 values.i 尚未对此进行测试,但请仔细阅读并让我们知道它是否有用。
<?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代码
<?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;
}
?>