如何将AWS SDK PHP用于AWSECommerceService

时间:2016-07-13 14:18:48

标签: php amazon-web-services amazon-s3 amazon-ec2

我必须使用以下API网址来获取图书信息(找到here) -

http://webservices.amazon.com/onca/xml?
  Service=AWSECommerceService
  &Operation=ItemLookup
  &ResponseGroup=Large
  &SearchIndex=All
  &IdType=ISBN
  &ItemId=076243631X
  &AWSAccessKeyId=[Your_AWSAccessKeyID]
  &AssociateTag=[Your_AssociateTag]
  &Timestamp=[YYYY-MM-DDThh:mm:ssZ]
  &Signature=[Request_Signature]

我可以使用PHP SDK,我找不到任何有关如何使用SDK实现此目的的文档。

修改

关注this& this链接,我写了以下代码 -

<?php

date_default_timezone_set('UTC');
$ItemId = 1603843698;
$ResponseGroup = 'Offers';
//$Timestamp = gmdate("Y-M-DTh:m:sZ");
$Timestamp = gmdate("Y-m-d\TH:i:s\Z");
$AWSAccessKeyId = "EXAMPLEEXAMPLE";

$associateTag = "something-10";

echo $Timestamp;
echo "<br />";
$str = "GET\n
webservices.amazon.com\n
/onca/xml\n
AWSAccessKeyId=".$AWSAccessKeyId."&AssociateTag=".$associateTag."&ItemId=".$ItemId."&Operation=ItemLookup&ResponseGroup=".$ResponseGroup."&Service=AWSECommerceService&Timestamp=".urlencode($Timestamp);
$str = urlencode(base64_encode(hash_hmac("sha256",$str,'my secrete',true)));

$url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=Offers&IdType=ASIN&ItemId=".$ItemId."&AssociateTag=ebooksprices-20&AWSAccessKeyId=".$AWSAccessKeyId."&Timestamp=".urlencode($Timestamp)."&Signature=".$str;
echo "<br />";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);

但是没有成功,我得到了这个输出 -

  

2016-07-14T09:50:06Z string(427)&#34; SignatureDoesNotMatch请求   我们计算的签名与您提供的签名不符。   检查您的AWS Secret Access Key和签名方法。咨询   服务文档   details.34b23224-4750-46c7-8f75-929239f955de&#34;

任何帮助将不胜感激。 感谢

2 个答案:

答案 0 :(得分:2)

AWS SDK目前不支持亚马逊产品广告API。

为了实现,亚马逊在此页面上提供了一个PHP代码示例:Implementing a Product Advertising API Request

对于请求签名,请遵循以下文档:http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html。有关请求签名的PHP实现的更多具体细节已在此处回答:Amazon Product API returns “SignatureDoesNotMatch”

答案 1 :(得分:1)

最后我有时间发布我的工作代码 -

$ItemId = $_GET['isbn'];
$ResponseGroup = 'Offers';
//$Timestamp = gmdate("Y-M-DTh:m:sZ");
//date_default_timezone_set('America/Los_Angeles');
$Timestamp = gmdate("Y-m-d\TH:i:s\Z");
$AWSAccessKeyId = "/your key/";

$AssociateTag = "/your-tag/";
$Version = "2013-08-01";
//echo $Timestamp;
//echo "<br />";

$str = "Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=".$ResponseGroup."&IdType=ASIN&ItemId=".urlencode($ItemId)."&AssociateTag=".$AssociateTag."&AWSAccessKeyId=".$AWSAccessKeyId."&Timestamp=".urlencode($Timestamp);

$ar = explode("&", $str);
//var_dump($ar);

natsort($ar);
//var_dump($ar);

$str = "GET
webservices.amazon.com
/onca/xml
";

$str .= implode("&", $ar); 

//echo $str;
//echo "<br />";
$str = urlencode(base64_encode(hash_hmac("sha256",$str,'/your secret/',true)));

$url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=Offers&IdType=ASIN&ItemId=".$ItemId."&AssociateTag=your-tag&AWSAccessKeyId=".$AWSAccessKeyId."&Timestamp=".urlencode($Timestamp)."&Signature=".$str;
//echo "<br />";
//echo $url;
//echo "<br />";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
//var_dump($result);

$xml=simplexml_load_string($result) or die("Error: Cannot create object");

echo "<h1>AMAZON API</h1><pre>";

print_r($xml);

此代码以XML格式返回响应。

由于