我知道有关这个主题的问题已被多次询问,我已阅读所有但仍无法解决我的问题。这是我的代码......
<?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);
?>
当我提交请求时,我收到以下错误:
SenderContentMD5DoesNotMatch您为Feed传递的Content-MD5 HTTP标头与我们为您的Feed4f0c5178-8971-4322-ba85-aed0bfe81bd0
计算的Content-MD5不匹配
实际上我正在更新MWS上的订单状态。
如果有人解决问题或指出我哪里错了,将不胜感激。
答案 0 :(得分:1)
您正在对xml进行修剪,然后计算md5
。而亚马逊没有对您发送的xml进行任何修剪。因此不匹配。从那里删除trim()
。
答案 1 :(得分:0)
我得到了解决方案。 从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生成正确的哈希值。
注意: stream_get_contents($ feedHandle)在亚马逊的php库中提到。