将amazon MWS暂存器查询转换为API调用

时间:2012-07-27 19:22:56

标签: php xml api amazon amazon-mws

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

e.g。当使用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
}

有没有人有任何可能的建议?

3 个答案:

答案 0 :(得分:13)

我也很努力地讨论这个问题,以下是我为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。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

我对这段代码有点麻烦,我把它上传到托管的Web服务器时工作但不是在使用windows和xampp运行本地时。

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

  

curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,0);

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