使用 PHP 的亚马逊 MWS API 没有显示结果 [英] Amazon MWS API using PHP shows no Result

查看:16
本文介绍了使用 PHP 的亚马逊 MWS API 没有显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 amazon mws API 来开发一个项目来查找最低价优惠,它适用于操作 ListMatchingProducts 和 GetMatchingProduct 但当涉及到 GetLowestPricedOffersForASIN 时,它在 XML 输出中没有显示结果

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

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

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

我的 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;

我不能把 AWSAccessKeyID、Secret 和 SellerID 放在这里...

I can't put AWSAccessKeyID, Secret and SellerID here...

推荐答案

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

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;

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

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.

这篇关于使用 PHP 的亚马逊 MWS API 没有显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆