【问题标题】:Converting amazon MWS scratchpad queries to API calls将亚马逊 MWS 暂存器查询转换为 API 调用
【发布时间】:2012-07-26 12:28:45
【问题描述】:

我想知道是否有办法将我的亚马逊 MWS scratchpad 查询转换为 API 调用

例如当使用 MWS 便签本时,我得到了一个要签名的字符串

   "mws.amazonservices.co.uk"
   ."/Products/2011-10-01"
   ."AWSAccessKeyId=xxx&Action=ListMatchingProducts"
   ."&MarketplaceId=xxx&Query=star%20wars&SellerId=xxx"
   ."&SignatureMethod=HmacSHA256&SignatureVersion=2
   ."&Timestamp=2012-07-27T18%3A59%3A30Z&Version=2011-10-01

花了几天时间试图让Amazons order API 工作后,我放弃了,一直希望下面的函数会返回一个 xml 字符串......但没有运气

function callAmazon(){
    $apicall =  "mws.amazonservices.co.uk"
   ."/Products/2011-10-01"
   ."AWSAccessKeyId=xxx&Action=ListMatchingProducts"
   ."&MarketplaceId=xxx&Query=star%20wars&SellerId=xxx"
   ."&SignatureMethod=HmacSHA256&SignatureVersion=2
   ."&Timestamp=2012-07-27T18%3A59%3A30Z&Version=2011-10-01   

    $resp = simplexml_load_file($apicall);   //make the call
}

有人有什么建议吗?

【问题讨论】:

    标签: php xml api amazon amazon-mws


    【解决方案1】:

    我也为此苦苦挣扎了很长时间,以下是我为 Products API 解决的方法:

    <?php
    require_once('.config.inc.php');
    $base_url = "https://mws.amazonservices.com/Products/2011-10-01";
    $method = "POST";
    $host = "mws.amazonservices.com";
    $uri = "/Products/2011-10-01";
    
    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' => "Books");
    
        // 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_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $response = curl_exec($ch);
    
        $parsed_xml = simplexml_load_string($response);
    
        return ($parsed_xml);
    }
    
    ?>
    

    .inc.config.php 文件包含我的访问密钥、密钥等。

    编辑:
    $searchterm 是我从表单中传递的 isbn。

    【讨论】:

    • 这看起来很有趣,我试一试,我需要做的就是获取我之前使用的配置文件
    • 我拿了新 api 自带的那个。
    • 函数没有返回任何东西?我还需要改变什么吗?
    • 您需要更改一些参数以满足您对所使用 API 的要求。您可以通过硬编码所有信息并使用 print_r($parsed_xml) 来测试它,这将向您显示 xml。
    • 没问题,就像我说的,我也为此苦苦挣扎了很长时间,其他人帮助了我。祝你好运,亚马逊文档非常缺乏。
    【解决方案2】:

    您需要实际调用 API。您使用的字符串未指定 URL 的 http:// 部分。

    【讨论】:

    • 所以当实际使用 http 或 https 时,你在 $resp 中得到什么结果?另外,您尝试在浏览器中或通过其他方法(即 cURL、wget 等)调用这些 URL 得到什么
    • $resp 只返回错误,但在浏览器中返回一个 XML 字符串
    • 可能是 XML 格式错误或者可能包含 CDATA 块,在这种情况下您需要使用 LIBXML_NOCDATA 选项。
    【解决方案3】:

    我在处理这段代码时遇到了一点问题,上传到托管网络服务器时它可以正常工作,但使用 windows 和 xampp 在本地运行时却不行。

    如果您遇到问题,请尝试将其添加到 curl-setopt 块的末尾。

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    顺便说一句,您还需要修改 xampp 文件夹中的 php.ini 文件以启用 curl。

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2019-04-19
      相关资源
      最近更新 更多