easy coder easy coder - 4 months ago 33
PHP Question

How to use AWS SDK PHP for AWSECommerceService

I have to use following API URL to fetch Books information (found 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]


I can use PHP SDK for this, I could not find any doc on how to achieve this using SDK.

EDIT

Following this & this links, I have written following code-

<?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);


But with no success, I get this output-


2016-07-14T09:50:06Z string(427) " SignatureDoesNotMatchThe request
signature we calculated does not match the signature you provided.
Check your AWS Secret Access Key and signing method. Consult the
service documentation for
details.34b23224-4750-46c7-8f75-929239f955de"


Any help will be appreciated.
Thanks

Answer

Finally I got time to post my working code-

$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);

This code returns the response in XML format.

Thanks

Comments