使用PHP POST对Amazon AMS API进行身份验证

时间:2015-03-29 21:06:25

标签: php api amazon

我从elsewhere 上获取了此代码。它的工作方式与广告一样。但是,我正在尝试用POST而不是GET做同样的事情,这显然是AMS推荐的,也就是这个特定的呼叫如何放在AMS Scratchpad上。

GET

配合使用的代码
<?php
$param = array();
$param['AWSAccessKeyId']   = 'YourAccessKeyID'; 
$param['Action']           = 'GetLowestOfferListingsForASIN'; 
$param['SellerId']         = 'YourSellerID'; 
$param['SignatureMethod']  = 'HmacSHA256';  
$param['SignatureVersion'] = '2'; 
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version']          = '2011-10-01'; 
$param['MarketplaceId']    = 'YourMarketplaceID'; 
$param['ItemCondition']    = 'new';
$param['ASINList.ASIN.1']  = 'B00C5XBAOA';
$secret = 'YourSecretKey';

$url = array();
foreach ($param as $key => $val) {

    $key = str_replace("%7E", "~", rawurlencode($key));
    $val = str_replace("%7E", "~", rawurlencode($val));
    $url[] = "{$key}={$val}";
}

sort($url);

$arr   = implode('&', $url);

$sign  = 'GET' . "\n";
$sign .= 'mws.amazonservices.com' . "\n";
$sign .= '/Products/2011-10-01' . "\n";
$sign .= $arr;

$signature = hash_hmac("sha256", $sign, $secret, true);
$signature = urlencode(base64_encode($signature));

$link  = "https://mws.amazonservices.com/Products/2011-10-01?";
$link .= $arr . "&Signature=" . $signature;
echo($link); //for debugging - you can paste this into a browser and see if it loads.

$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo('<p>' . $response . '</p>');
print_r('<p>' . $info . '</p>');
?>

不适用于POST

的代码
<?php
$post = '';
$param = array();
$param['AWSAccessKeyId']   = 'YourAccessKeyID'; 
$param['Action']           = 'GetLowestOfferListingsForASIN'; 
$param['SellerId']         = 'YourSellerID'; 
$param['SignatureMethod']  = 'HmacSHA256';  
$param['SignatureVersion'] = '2'; 
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version']          = '2011-10-01'; 
$param['MarketplaceId']    = 'YourMarketplaceID'; 
$param['ItemCondition']    = 'new';
$param['ASINList.ASIN.1']  = 'B00C5XBAOA';
$secret = 'YourSecretKey';

$url = array();
foreach ($param as $key => $val) {

    $key = str_replace("%7E", "~", rawurlencode($key));
    $val = str_replace("%7E", "~", rawurlencode($val));
    $url[] = "{$key}={$val}";
}

sort($url);

$arr   = implode('&', $url);

$sign  = 'POST' . "\n";
$sign .= 'mws.amazonservices.com' . "\n";
$sign .= '/Products/2011-10-01' . "\n";
$sign .= $arr;

$signature = hash_hmac("sha256", $sign, $secret, true);
$signature = urlencode(base64_encode($signature));
$post .= $arr . "&Signature=" . $signature;
echo $post;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://mws.amazonservices.com/Products/2011-10-01");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '$post');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$response = simplexml_load_string($response);
print_r($response);
//print_r($info);

?>

我收到以下回复:

(
    [Error] => SimpleXMLElement Object
        (
            [Type] => Sender
            [Code] => InvalidParameterValue
            [Message] => Either Action or Operation query parameter must be present.
        )

    [RequestID] => b6d5ee02-6ace-4482-92b7-xxxxxxxxxx
)

我相信我在签名身份验证方面做错了。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

我修复了POST脚本。

您必须在curl选项中删除CURLOPT_HTTPHEADER,并在CURLOPT_POSTFIELDS上的$ post上删除que单引号。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://mws.amazonservices.es/Products/2011-10-01");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

header("Content-Type: text/xml;charset=UTF-8");
echo($response);

萨吕, Josep M