Amazon Marketplace feed - Issue in calculating MD5 hash
<p>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...</p>

<pre><code>&lt;?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 = '
    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd"&gt;
          &lt;Header&gt;
               &lt;DocumentVersion&gt;1.02&lt;/DocumentVersion&gt;
               &lt;MerchantIdentifier&gt;Mystore&lt;/MerchantIdentifier&gt;
          &lt;/Header&gt;
          &lt;MessageType&gt;OrderFulfillment&lt;/MessageType&gt;
          &lt;Message&gt;
               &lt;MessageID&gt;123456&lt;/MessageID&gt;
               &lt;OrderFulfillment&gt;
                     &lt;AmazonOrderID&gt;123-1234567-1234567&lt;/AmazonOrderID&gt;
                     &lt;MerchantFulfillmentID&gt;1474290171&lt;/MerchantFulfillmentID&gt;
                     &lt;FulfillmentDate&gt;2016-09-19T12:23:41+00:00&lt;/FulfillmentDate&gt;
                     &lt;FulfillmentData&gt;
                            &lt;CarrierCode&gt;FedEx&lt;/CarrierCode&gt;
                            &lt;ShippingMethod&gt;FedEx&lt;/ShippingMethod&gt;
                            &lt;ShipperTrackingNumber&gt;1111111&lt;/ShipperTrackingNumber&gt;
                     &lt;/FulfillmentData&gt;

               &lt;/OrderFulfillment&gt;
          &lt;/Message&gt;
      &lt;/AmazonEnvelope&gt;
   ';

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

$params = array(
    'AWSAccessKeyId' =&gt; AWS_ACCESS_KEY_ID,
    'Action' =&gt; "SubmitFeed",
    'Merchant' =&gt; MERCHANT_ID,
    'SignatureVersion' =&gt; "2",
    'Timestamp'=&gt; gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()),
    'Version'=&gt; "2009-01-01",
    'SignatureMethod' =&gt; "HmacSHA256",
    'FeedType' =&gt; '_POST_ORDER_FULFILLMENT_DATA_',
    'MarketplaceIdList.Id.1' =&gt; MARKETPLACE_ID,
    'PurgeAndReplace' =&gt; '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("&amp;", $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 . "&amp;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, '&lt;MWS_SubmitFeed&gt;/&lt;1.02&gt; (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);
?&gt;
</code></pre>

<p>When I submit the request I am getting the following error:</p>

<blockquote>
  <p>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</p>
</blockquote>

<p>Actually i am updating the order status on MWS.</p>

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