Amazon Marketplace 提要 - 计算 MD5 哈希时出现问题
Amazon Marketplace feed - Issue in calculating MD5 hash
我知道与此主题相关的问题已被问过很多次,我已全部阅读但仍无法解决我的问题。这是我的代码...
<?php
define('AWS_ACCESS_KEY_ID','xxxxx');
define('AWS_SECRET_ACCESS_KEY','xxxxx');
define('MERCHANT_ID','xxxxx');
define('MARKETPLACE_ID','xxxxxx');
$file = 'test.xml';
$feed = '
<?xml version="1.0" encoding="UTF-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.02</DocumentVersion>
<MerchantIdentifier>Mystore</MerchantIdentifier>
</Header>
<MessageType>OrderFulfillment</MessageType>
<Message>
<MessageID>123456</MessageID>
<OrderFulfillment>
<AmazonOrderID>123-1234567-1234567</AmazonOrderID>
<MerchantFulfillmentID>1474290171</MerchantFulfillmentID>
<FulfillmentDate>2016-09-19T12:23:41+00:00</FulfillmentDate>
<FulfillmentData>
<CarrierCode>FedEx</CarrierCode>
<ShippingMethod>FedEx</ShippingMethod>
<ShipperTrackingNumber>1111111</ShipperTrackingNumber>
</FulfillmentData>
</OrderFulfillment>
</Message>
</AmazonEnvelope>
';
$feedHandle = @fopen($file, 'w');
fwrite($feedHandle, trim($feed));
rewind($feedHandle);
$params = array(
'AWSAccessKeyId' => AWS_ACCESS_KEY_ID,
'Action' => "SubmitFeed",
'Merchant' => MERCHANT_ID,
'SignatureVersion' => "2",
'Timestamp'=> gmdate("Y-m-d\TH:i:s.\0\0\0\Z", time()),
'Version'=> "2009-01-01",
'SignatureMethod' => "HmacSHA256",
'FeedType' => '_POST_ORDER_FULFILLMENT_DATA_',
'MarketplaceIdList.Id.1' => MARKETPLACE_ID,
'PurgeAndReplace' => 'false',
);
$url_parts = array();
foreach(array_keys($params) as $key)
$url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key]));
sort($url_parts);
$url_string = implode("&", $url_parts);
$string_to_sign = "POST\nmws.amazonservices.com\n/Feeds/2009-01-01\n" . $url_string;
// Sign the request
$signature = hash_hmac("sha256", $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);
$signature = urlencode(base64_encode($signature));
$url = "https://mws.amazonservices.com/Feeds/2009-01-01" . '?' . $url_string . "&Signature=" . $signature;
$md5 = base64_encode(md5(trim($feed), true));
$httpHeader=array();
$httpHeader[]='Transfer-Encoding: chunked';
$httpHeader[]='Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$httpHeader[]='Content-MD5: ' . $md5;
$httpHeader[]='Expect:';
$httpHeader[]='Accept:';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_USERAGENT, '<MWS_SubmitFeed>/<1.02> (Language=PHP/' . phpversion() . ')');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_INFILE, $feedHandle);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
var_dump($response);
?>
当我提交请求时出现以下错误:
SenderContentMD5DoesNotMatchthe Content-MD5 HTTP header you passed for your feed did not match the Content-MD5 we calculated for your feed4f0c5178-8971-4322-ba85-aed0bfe81bd0
实际上我正在更新 MWS 上的订单状态。
如果有人能解决问题或指出我哪里错了,将不胜感激。
您正在 xml 上执行 trim,然后计算 md5
。而亚马逊没有对您发送的 xml 做任何 trim。因此不匹配。从那里删除 trim()
。
我得到了解决方案。
从 Sabuj Hassan 建议的这些行中删除 trim() 并修改哈希生成代码:
fwrite($feedHandle, trim($feed));
至
fwrite($feedHandle, $feed);
和
$md5 = base64_encode(md5(trim($feed), true));
至
$md5 = base64_encode(md5(stream_get_contents($feedHandle), true));
将 trim($feed) 替换为 stream_get_contents($feedHandle),它将为 content-md5 生成正确的哈希值。
Note: stream_get_contents($feedHandle) is mentioned in amazon's php library.
我知道与此主题相关的问题已被问过很多次,我已全部阅读但仍无法解决我的问题。这是我的代码...
<?php
define('AWS_ACCESS_KEY_ID','xxxxx');
define('AWS_SECRET_ACCESS_KEY','xxxxx');
define('MERCHANT_ID','xxxxx');
define('MARKETPLACE_ID','xxxxxx');
$file = 'test.xml';
$feed = '
<?xml version="1.0" encoding="UTF-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.02</DocumentVersion>
<MerchantIdentifier>Mystore</MerchantIdentifier>
</Header>
<MessageType>OrderFulfillment</MessageType>
<Message>
<MessageID>123456</MessageID>
<OrderFulfillment>
<AmazonOrderID>123-1234567-1234567</AmazonOrderID>
<MerchantFulfillmentID>1474290171</MerchantFulfillmentID>
<FulfillmentDate>2016-09-19T12:23:41+00:00</FulfillmentDate>
<FulfillmentData>
<CarrierCode>FedEx</CarrierCode>
<ShippingMethod>FedEx</ShippingMethod>
<ShipperTrackingNumber>1111111</ShipperTrackingNumber>
</FulfillmentData>
</OrderFulfillment>
</Message>
</AmazonEnvelope>
';
$feedHandle = @fopen($file, 'w');
fwrite($feedHandle, trim($feed));
rewind($feedHandle);
$params = array(
'AWSAccessKeyId' => AWS_ACCESS_KEY_ID,
'Action' => "SubmitFeed",
'Merchant' => MERCHANT_ID,
'SignatureVersion' => "2",
'Timestamp'=> gmdate("Y-m-d\TH:i:s.\0\0\0\Z", time()),
'Version'=> "2009-01-01",
'SignatureMethod' => "HmacSHA256",
'FeedType' => '_POST_ORDER_FULFILLMENT_DATA_',
'MarketplaceIdList.Id.1' => MARKETPLACE_ID,
'PurgeAndReplace' => 'false',
);
$url_parts = array();
foreach(array_keys($params) as $key)
$url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key]));
sort($url_parts);
$url_string = implode("&", $url_parts);
$string_to_sign = "POST\nmws.amazonservices.com\n/Feeds/2009-01-01\n" . $url_string;
// Sign the request
$signature = hash_hmac("sha256", $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);
$signature = urlencode(base64_encode($signature));
$url = "https://mws.amazonservices.com/Feeds/2009-01-01" . '?' . $url_string . "&Signature=" . $signature;
$md5 = base64_encode(md5(trim($feed), true));
$httpHeader=array();
$httpHeader[]='Transfer-Encoding: chunked';
$httpHeader[]='Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$httpHeader[]='Content-MD5: ' . $md5;
$httpHeader[]='Expect:';
$httpHeader[]='Accept:';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_USERAGENT, '<MWS_SubmitFeed>/<1.02> (Language=PHP/' . phpversion() . ')');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_INFILE, $feedHandle);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
var_dump($response);
?>
当我提交请求时出现以下错误:
SenderContentMD5DoesNotMatchthe Content-MD5 HTTP header you passed for your feed did not match the Content-MD5 we calculated for your feed4f0c5178-8971-4322-ba85-aed0bfe81bd0
实际上我正在更新 MWS 上的订单状态。
如果有人能解决问题或指出我哪里错了,将不胜感激。
您正在 xml 上执行 trim,然后计算 md5
。而亚马逊没有对您发送的 xml 做任何 trim。因此不匹配。从那里删除 trim()
。
我得到了解决方案。 从 Sabuj Hassan 建议的这些行中删除 trim() 并修改哈希生成代码:
fwrite($feedHandle, trim($feed));
至
fwrite($feedHandle, $feed);
和
$md5 = base64_encode(md5(trim($feed), true));
至
$md5 = base64_encode(md5(stream_get_contents($feedHandle), true));
将 trim($feed) 替换为 stream_get_contents($feedHandle),它将为 content-md5 生成正确的哈希值。
Note: stream_get_contents($feedHandle) is mentioned in amazon's php library.