zed zed - 1 year ago 172
PHP Question

Amazon Marketplace feed - Issue in calculating MD5 hash

I know that the question related to this topic has been asked many times and i have read all but still not able to solve my problem. Here is my code...


$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">


$feedHandle = @fopen($file, 'w');
fwrite($feedHandle, trim($feed));

$params = array(
'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",
'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]));


$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[]='Transfer-Encoding: chunked';
$httpHeader[]='Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$httpHeader[]='Content-MD5: ' . $md5;

$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);

When I submit the request I am getting the following error:

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

Actually i am updating the order status on MWS.

Would be appreciated if anyone sort out the problem or point out that where i am going to wrong.

Answer Source

You are doing a trim on the xml and then calculating the md5. Whereas the amazon is not doing any trim on the xml that you send. Hence the mismatch. Remove that trim() from there.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download