2012-07-27 33 views
11

我想知道是否有办法我亚马逊MWS转换scratchpad查询通过API调用转换亚马逊MWS暂存器查询API调用

例如使用MWS暂存器,当我给一个String签署

"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 
} 

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

回答

13

我这个挣扎了很长一段时间为好,这里是我如何解决它的产品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。

+0

这看起来很有趣我会给它一个镜头,我所需要做的就是抓住之前使用的配置文件 – 2012-07-27 19:39:44

+1

我采用了新的apis附带的配置文件。 – Jim 2012-07-27 19:42:45

+0

函数没有返回任何东西?我必须改变什么吗? – 2012-07-27 19:44:34

1

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

+0

我也尝试在字符串 – 2012-07-27 19:32:39

+1

的开头添加“https://”,实际使用http或https时,您在$ resp中获得了什么结果?你也可以尝试在浏览器或其他方法(如cURL,wget等)中调用这些URL – 2012-07-27 19:35:11

+0

$ resp只返回错误,但在浏览器中返回一个XML字符串 – 2012-07-27 19:36:29

1

我对这段代码有点麻烦,当它上传到托管的Web服务器时,我没有在运行本地窗口和xampp时使用它。

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

curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,0);

btw您还需要修改xampp文件夹中的php.ini文件以启用卷曲。