【问题标题】:Cannot retrieve results from amazon mws products api无法从 amazon mws products api 检索结果
【发布时间】:2016-12-26 22:35:51
【问题描述】:

使用下面的代码,我能得到的唯一结果是 URL。当我复制并粘贴到浏览器中时,我得到:

<Message>

我们计算的请求签名与您提供的签名不匹配。检查您的 AWS 秘密访问密钥和签名方法。有关详细信息,请参阅服务文档。

正在使用的代码;

<?php
date_default_timezone_set('America/New_York');
define('AWS_ACCESS_KEY_ID', '***');
define('AWS_SECRET_ACCESS_KEY', '***');
define('APPLICATION_NAME', 'test');
define('APPLICATION_VERSION', '1.0');
define ('MERCHANT_ID', '***');
define ('MARKETPLACE_ID', '***');

$base_url = "https://mws.amazonservices.com/Products/2011-10-01";
$method = "GET";
$host = "mws.amazonservices.com";
$uri = "/Products/2011-10-01";

function amazon_xml($searchTerm) {

$params = array(
    'AWSAccessKeyId' => AWS_ACCESS_KEY_ID,
    'Action' => "GetCompetitivePricingForASIN",
    'SellerId' => MERCHANT_ID,
    'SignatureMethod' => "HmacSHA256",
    'SignatureVersion' => "2",
    'Timestamp'=> gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()),
    'Version'=> "2011-10-01",
    'MarketplaceId' => MARKETPLACE_ID,
    'Query' => $searchTerm,
    'QueryContextId' => "Automotive");

// Sort the URL parameters
$url_parts = array();
foreach(array_keys($params) as $key)
$url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key]));
sort($url_parts);

// Construct the string to sign
$url_string = implode("&", $url_parts);
$string_to_sign = "GET\nmws.amazonservices.com\n/Products/2011-10-01\n" . $url_string;

// Sign the request
$signature = hash_hmac("sha256", $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);

// Base64 encode the signature and make it URL safe
$signature = urlencode(base64_encode($signature));

$url = "https://mws.amazonservices.com/Products/2011-10-01" . '?' . $url_string .           "&Signature=" . $signature;


$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($ch);

echo $url;
// return $xml if you still want to parse the data elsewhere
$xml = simplexml_load_string($response);

// return this if you just want the raw XML string
return $xml->asXML();
echo $xml->GetMatchingProductResult->Product->Identifiers->MarketplaceASIN->ASIN;
}

$searchTerm = "B00FNNLBK2";

amazon_xml($searchTerm);



//foreach ($xml->GetMatchingProductResult->Product as $product) {
// do something for each <Product>, such as output the ASIN...

//}



echo $xml;
echo $searchTerm;
echo $xml->GetMatchingProductResult->Product->Identifiers->MarketplaceASIN->ASIN;
echo $url;
?>

我确实多次验证了登录凭据是否正确,没有抽象的前导或尾随空格。

【问题讨论】:

    标签: php amazon-mws


    【解决方案1】:

    这些参数(例如QueryQueryContextId)与您尝试调用的Action (GetCompetitivePricingForASIN) 不匹配。它们对应于ListMatchingProducts 操作。

    假设你的意思是 ListMatchingProducts 试试这个:

    <?php
    date_default_timezone_set('America/New_York');
    define('AWS_ACCESS_KEY_ID', '***');
    define('AWS_SECRET_ACCESS_KEY', '***');
    define('APPLICATION_NAME', 'test');
    define('APPLICATION_VERSION', '1.0');
    define ('MERCHANT_ID', '***');
    define ('MARKETPLACE_ID', '***');
    function amazon_xml($searchTerm)
    {
    
        $params = array(
            'AWSAccessKeyId' => AWS_ACCESS_KEY_ID,
            'Action' => "ListMatchingProducts",
            'SellerId' => MERCHANT_ID,
            'SignatureMethod' => "HmacSHA256",
            'SignatureVersion' => "2",
            'Timestamp' => gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()),
            'Version' => "2011-10-01",
            'MarketplaceId' => MARKETPLACE_ID,
            'Query' => $searchTerm,
            'QueryContextId' => "Automotive"
        );
        $url_parts = array();
        foreach (array_keys($params) as $key)
            $url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key]));
        sort($url_parts);
        $url_string     = implode("&", $url_parts);
        $string_to_sign = "GET\nmws.amazonservices.com\n/Products/2011-10-01\n" . $url_string;
        $signature = hash_hmac("sha256", $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);
        $signature = urlencode(base64_encode($signature));
        $url = "https://mws.amazonservices.com/Products/2011-10-01" . '?' . $url_string . "&Signature=" . $signature;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $response = curl_exec($ch);
        $xml      = simplexml_load_string($response);
        return $xml->asXML();
    }
    $searchTerm = "B00FNNLBK2";
    echo amazon_xml($searchTerm);
    

    您可以在 MWS 文档中找到每个 Action 的相关参数,例如这里: http://docs.developer.amazonservices.com/en_US/products/Products_GetCompetitivePricingForASIN.html

    您还可以使用 MWS Scratchpad 查看它们: https://mws.amazonservices.com/scratchpad/index.html

    【讨论】:

    猜你喜欢
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多