简体   繁体   English

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

[英]Amazon MWS API using PHP shows no Result

I have been using amazon mws API for developing a project to find Lowest Priced offers, It works fine with action ListMatchingProducts and GetMatchingProduct but when it comes to GetLowestPricedOffersForASIN, it shows no result in XML output我一直在使用 amazon mws API 来开发一个项目来查找最低价优惠,它适用于 ListMatchingProducts 和 GetMatchingProduct 操作,但是当涉及到 GetLowestPricedOffersForASIN 时,它在 XML 输出中没有显示任何结果

"This XML file does not appear to have any style information associated with it. The document tree is shown below." “这个 XML 文件似乎没有任何与之相关的样式信息。文档树如下所示。”

My PHP File Here:我的 PHP 文件在这里:

 <?php $param = array(); $param['AWSAccessKeyId'] = ''; $param['Action'] = 'GetLowestPricedOffersForASIN'; $param['SellerId'] = ''; $param['SignatureMethod'] = 'HmacSHA256'; $param['SignatureVersion'] = '2'; $param['Timestamp'] = gmdate("Ymd\\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;
I can't put AWSAccessKeyID, Secret and SellerID here... 我不能把 AWSAccessKeyID、Secret 和 SellerID 放在这里...

Your main problem is, that you use GET instead of POST.您的主要问题是,您使用 GET 而不是 POST。 This version of your code works:此版本的代码有效:

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

I have used curl_setopt($ch, CURLOPT_VERBOSE, true);我用过curl_setopt($ch, CURLOPT_VERBOSE, true); to debug the response from server.调试来自服务器的响应。 Your code did not produced any http body to output, but this http header HTTP/1.1 405 Method Not Allowed .您的代码没有生成任何要输出的 http 正文,但是此 http 标头HTTP/1.1 405 Method Not Allowed Changing to POST solved your problem.更改为 POST 解决了您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM