亚马逊MWS提交Feed平面文件产品重定价问题

时间:2017-08-28 06:46:04

标签: php amazon-web-services curl amazon-mws

我在amazon mws API中使用php curl重新定价产品时遇到问题,有错误 发件人ContentMD5DoesNotMatch 您为Feed传递的Content-MD5 HTTP标头与我们为您的Feed4bad39e1-7479-4ebb-ae94-76e2eed742cb计算的Content-MD5不匹配

这是完整的代码

fwrite($ write,“sku \ tprice”);

while($row = mysql_fetch_assoc($rsSql))
{
    fwrite($write,"\n".$row['seller_sku']."\t".$row['current_price']);
}

fclose($write);

$file = 'prices.tsv';
$fo = fopen($file,'r');

$httpHeader=array();
$httpHeader[]='Transfer-Encoding: chunked';
$httpHeader[]='Content-Type: text/tab-separated-values';
$httpHeader[]='Content-MD5: ' . base64_encode(md5(trim($file)));
$httpHeader[]='Expect:';
$httpHeader[]='Accept:';

$curl_options=array(
CURLOPT_UPLOAD=>true,
CURLOPT_INFILE=>$fo,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_POST=>true,
// CURLOPT_PORT=>443,
// CURLOPT_SSLVERSION=>3,
CURLOPT_SSL_VERIFYHOST=>false,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_FOLLOWLOCATION=>1,
CURLOPT_PROTOCOLS=>CURLPROTO_HTTPS,
CURLINFO_HEADER_OUT=>TRUE,
CURLOPT_HTTPHEADER=>$httpHeader,
CURLOPT_CUSTOMREQUEST=>'POST',
CURLOPT_VERBOSE=>true,
// CURLOPT_HEADER=>true,
);


$param = array();
$param['AWSAccessKeyId']   = AWSKEY; 
$param['Action']           = 'SubmitFeed'; //CHANGE THIS
$param['SellerId']         = SELLERID; 
$param['SignatureMethod']  = 'HmacSHA256';  
$param['SignatureVersion'] = '2'; 
$param['ContentMD5Value']  = base64_encode(md5(trim($file)));
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version']          = '2009-01-01'; 
$param['MarketplaceId']    = MARKETPLACE; 
$param['FeedType']    = "_POST_FLAT_FILE_PRICEANDQUANTITYONLY_UPDATE_DATA_";
$param['PurgeAndReplace']    = 'true';


$secret = SECRETKEY;

$url = array();
foreach ($param as $key => $val) {

    $key = str_replace("%7E", "~", rawurlencode($key));
    $val = str_replace("%7E", "~", rawurlencode($val));
    $url[] = "{$key}={$val}";

}

sort($url);
$arr = implode('&', $url);

$sign = 'POST' . "\n";
$sign .= 'mws.amazonservices.com' . "\n";
$sign .= '/Feeds/2009-01-01' . "\n";
$sign .= $arr;

$signature = hash_hmac("sha256", $sign, $secret, true);
$signature = urlencode(base64_encode($signature));
$link = "https://mws.amazonservices.com/Feeds/2009-01-01?";
$link .= $arr . "&Signature=" . $signature;

$ch = curl_init($link);
curl_setopt_array($ch,$curl_options);
$response=curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

$xml = simplexml_load_string("$response");
echo $response;

1 个答案:

答案 0 :(得分:0)

您的代码将计算文件的名称的MD5,而不是其内容。

 base64_encode(md5(trim($file))); <-- $file is actually the filename!

此外,您不应修剪文件内容中的任何内容,并且需要将二进制MD5传递给编码器:

 base64_encode(md5($filecontents,true));

 base64_encode(md5_file($filename,true));