2016-09-19 37 views
2

我知道與此主題相關的問題已被多次詢問,並且我已閱讀所有內容,但仍無法解決我的問題。這裏是我的代碼...Amazon Marketplace飼料 - 計算MD5散列的問題

<?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的訂單狀態。

如果有人解決了這個問題或者指出我會犯錯的地方,我將不勝感激。

回答

1

您正在對xml進行修剪,然後計算md5。而亞馬遜沒有對您發送的xml進行任何修剪。因此不匹配。從那裏刪除那個trim()

+0

沒有運氣,去除裝飾(),仍然得到同樣的錯誤,並呼應MD5時後,它打印這樣的:GFhBmvap + b32ZJS7MLvRzQ == – zed

+0

好吧,我得到了解決。 – zed

0

我得到了解決方案。 從這些行由Sabuj哈桑建議拆下裝飾件()和改變哈希生成代碼:

fwrite($feedHandle, trim($feed)); 

fwrite($feedHandle, $feed); 

$md5 = base64_encode(md5(trim($feed), true)); 

$md5 = base64_encode(md5(stream_get_contents($feedHandle), true)); 

Re將trim($ feed)放到stream_get_contents($ feedHandle)中,它會爲content-md5生成正確的散列。

Note:stream_get_contents($feedHandle) is mentioned in amazon's php library.