douchou8935 2014-06-25 18:08
浏览 88
已采纳

无法检索amazon mws产品api的结果

Using the below code, the only result I can get to echo is the URL. When I copy and paste into browser, I get:

<Message>

The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

Code Being Used;

<?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
mws.amazonservices.com
/Products/2011-10-01
" . $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;
?>

I did verify multiple times that the sign in credentials are correct, with no absract leading or trailing space.

  • 写回答

1条回答 默认 最新

  • douxin5953 2014-06-26 05:36
    关注

    Those parameters (e.g. Query and QueryContextId) don't match with the Action you are trying to call (GetCompetitivePricingForASIN). They correspond to the ListMatchingProducts action.

    Assuming that you meant ListMatchingProducts try this:

    <?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
    mws.amazonservices.com
    /Products/2011-10-01
    " . $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);
    

    You can find the relevant parameters for each Action in the MWS documentation e.g. here: http://docs.developer.amazonservices.com/en_US/products/Products_GetCompetitivePricingForASIN.html

    You can also see them using the MWS Scratchpad: https://mws.amazonservices.com/scratchpad/index.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 NeRF的代码复现以及人工智能入门
  • ¥15 centos如何做一个可以让ikuai连接的openvpn
  • ¥15 Protege 中的Entities下面的界面删除了如何复原
  • ¥15 scrapy爬虫求帮
  • ¥15 imageEnView绘图问题
  • ¥15 关于#python#的问题:您好可以加您一下联系方式吗,在复现的时候确实有点问题难以解决
  • ¥15 联想电脑重装系统时无法发现硬盘
  • ¥15 MATLAB与UR10e实体机械臂建立通讯
  • ¥15 c++题需要快一点不用opencv
  • ¥15 关于#java#的问题:想要咨询Flowable流程引擎框架的问题