使用PHP的亚马逊MWS API没有显示结果

时间:2015-10-26 08:17:41

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

我一直在使用amazon mws API开发项目来查找最低价格的商品,它可以通过动作ListMatchingProducts和GetMatchingProduct工作正常但是当涉及到GetLowestPricedOffersForASIN时,它在XML输出中没有显示结果

"此XML文件似乎没有与之关联的任何样式信息。文档树如下所示。"

我的PHP文件:



<?php
$param = array();
$param['AWSAccessKeyId']   = ''; 
$param['Action']           = 'GetLowestPricedOffersForASIN';
$param['SellerId']         = ''; 
$param['SignatureMethod']  = 'HmacSHA256';  
$param['SignatureVersion'] = '2'; 
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version']          = '2011-10-01'; 
$param['MarketplaceId']    = 'ATVPDKIKX0DER'; 
$param['ItemCondition']    = 'used';
$param['ASIN']			   = '0439139600';
$secret = '';
$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  = 'GET' . "\n";
$sign .= 'mws.amazonservices.com' . "\n";
$sign .= '/Products/2011-10-01' . "\n";
$sign .= $arr;

$signature = hash_hmac("sha256", $sign, $secret, true);
$signature = urlencode(base64_encode($signature));

$link  = "https://mws.amazonservices.com/Products/2011-10-01?";
$link .= $arr . "&Signature=" . $signature;
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
header('Content-type:text/xml');
echo $response;
&#13;
&#13;
&#13; 我不能把AWSAccessKeyID,Secret和SellerID放在这里......

1 个答案:

答案 0 :(得分:2)

您的主要问题是,您使用GET而不是POST。 此版本的代码有效:

$param = array();
$param['AWSAccessKeyId']   = ''; 
$param['Action']           = 'GetLowestPricedOffersForASIN';
$param['MarketplaceId']    = 'A1PA6795UKMFR9';
$param['SellerId']         = ''; 
$param['ASIN']             = 'B002BYQIWM'; 
$param['ItemCondition']    = 'New'; 
$param['SignatureMethod']  = 'HmacSHA256';  
$param['SignatureVersion'] = '2'; 
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version']          = '2011-10-01'; 
$secret = '';
$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.de' . "\n";
$sign .= '/Products/2011-10-01' . "\n";
$sign .= $arr;

$signature = hash_hmac("sha256", $sign, $secret, true);
$s64 = base64_encode($signature);

$signature = urlencode($s64);
$link  = "https://mws.amazonservices.de/Products/2011-10-01";
$arr .= "&Signature=" . $signature;

$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Accept:'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $response;

我使用curl_setopt($ch, CURLOPT_VERBOSE, true);来调试服务器的响应。您的代码未生成任何要输出的http正文,但此http标头HTTP/1.1 405 Method Not Allowed。更改为POST解决了您的问题。