gpt4 book ai didi

php - 从 Scratchpad 请求的亚马逊 MWS ListOrders

转载 作者:行者123 更新时间:2023-12-01 14:44:53 25 4
gpt4 key购买 nike

我正在尝试通过 MWS Scratchpad 获取订单列表。在暂存器中一切正常。

HTTP POST 是

POST /Orders/2013-09-01?AWSAccessKeyId=$CHIAVE_ACCESSO
&Action=ListOrders
&SellerId=$SELLER_ID
&SignatureVersion=2
&Timestamp=2016-11-29T18%3A58%3A52Z
&Version=2013-09-01
&Signature=$SIGNATURE
&SignatureMethod=HmacSHA256
&CreatedAfter=2016-10-31T23%3A00%3A00Z
&MarketplaceId.Id.1=APJ6JRA9NG5V4 HTTP/1.1
Host: mws.amazonservices.it
x-amazon-user-agent: AmazonJavascriptScratchpad/1.0 (Language=Javascript)
Content-Type: text/xml

要签名的字符串(在第二个框中)是

POST
mws.amazonservices.it
/Orders/2013-09-01
AWSAccessKeyId=$CHIAVE_ACCESSO&Action=ListOrders&CreatedAfter=2016-10-31T23%3A00%3A00Z&MarketplaceId.Id.1=APJ6JRA9NG5V4&SellerId=$SELLER_ID&Signat ureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2016-11-29T18%3A58%3A52Z&Version=2013-09-01

显示暂存器的结果是正确的。

我想做的是通过 PHP 发出请求并详细说明结果。

但是如果我先试着把请求放在我的浏览器上

https://mws.amazonservices.it/Orders/2013-09-01?AWSAccessKeyId= $CHIAVE_ACCESSO&Action=ListOrders&MarketplaceId=APJ6JRA9NG5V4&SellerId=$SELLER_ID&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2016-11-29T19%3A13%3A01.000Z&Version=2013-09-01&Signature=Q9Xnr9JhtkzeLUAsCFKPln8SS34FkCQRmELE2WiIhPo%3D&CreatedAfter=2016-10-31T23%3A00%3A00Z

错误是 error

我用来创建签名的方法是在 stackoverflow 上找到的,如下所示:

$sign  = 'GET' . "\n";
$sign .= 'mws.amazonservices.it' . "\n";
$sign .= '/Orders/2013-09-01' . "\n";
$sign .= $arr;

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

我做错了什么?

最佳答案

参数的顺序很重要。

下面是一些示例代码,您应该可以从中着手。您只需稍微修改它以适应您的系统。

class AmazonMWS
{
private $secretKey = '';

private $parameters = array();

/**
* Constructor for the AmazonMWS class.
* Initializes constants.
*/
public function __construct()
{
$this->secretKey = Constant::get('SECRET_KEY');

$this->parameters['AWSAccessKeyId'] = Constant::get('AWSAccessKeyId');
$this->parameters['MarketplaceId.Id.1'] = Constant::get('MarketplaceId.Id.1');
$this->parameters['SellerId'] = Constant::get('SellerId');
$this->parameters['SignatureMethod'] = Constant::get('SignatureMethod');
$this->parameters['SignatureVersion'] = Constant::get('SignatureVersion');
}

public function setListOrders()
{
$this->parameters['Action'] = 'ListOrders';
$this->parameters['Version'] = '2013-09-01';
$this->parameters['Timestamp'] = $this->getTimestamp();

// this part should change and depend on the method/parameter.. for now just for testing

$this->parameters['CreatedAfter'] = '2015-11-01';
}

public function listOrders()
{
$request = "https://mws.amazonservices.com/Orders/2013-09-01?";
$request .= $this->getParameterString($this->parameters) . "&Signature=" . $this->calculateSignature($this->calculateStringToSign($this->parameters));

echo $request;

return Curl::fetchSSL($request);
}

/**
* Calculates String to sign.
*
* @param array $parameters request parameters
* @return String to sign
*/
protected function calculateStringToSign(array $parameters)
{
$stringToSign = 'GET' . "\n";
$stringToSign .= 'mws.amazonservices.com' . "\n";
$stringToSign .= '/Orders/2013-09-01' . "\n";
$stringToSign .= $this->getParameterString($parameters);

return $stringToSign;
}

/**
* Gets the query parameters as a String sorted in natural-byte order.
*
* @param array $parameters request parameters
* @return String of parameters
*/
protected function getParameterString(array $parameters)
{
$url = array();
foreach ($parameters as $key => $val) {
$key = $this->urlEncode($key);
$val = $this->urlEncode($val);
$url[] = "{$key}={$val}";
}
sort($url);

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

return $parameterString;
}

/**
* Computes RFC 2104-compliant HMAC signature.
*
* @param String to sign
*/
protected function calculateSignature($stringToSign)
{
$signature = hash_hmac("sha256", $stringToSign, $this->secretKey, true);
return urlencode(base64_encode($signature));
}

/**
* URL encodes a string.
*/
protected function urlEncode($string)
{
return str_replace("%7E", "~", rawurlencode($string));
}

/**
* Gets the current date as ISO 8601 timestamp
*/
protected function getTimestamp()
{
return gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
}
}

关于php - 从 Scratchpad 请求的亚马逊 MWS ListOrders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40874090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com