Amazon MWS API using PHP shows no Result

三世轮回 提交于 2019-12-12 17:26:44

问题


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

"This XML file does not appear to have any style information associated with it. The document tree is shown below."

My PHP File Here:

<?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;
I can't put AWSAccessKeyID, Secret and SellerID here...

回答1:


Your main problem is, that you use GET instead of 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); 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. Changing to POST solved your problem.



来源:https://stackoverflow.com/questions/33341235/amazon-mws-api-using-php-shows-no-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!