Php 将amazon MWS草稿行查询转换为API调用
我想知道是否有办法将我的Amazon MWS查询转换为API调用 e、 g.当使用MWS草稿行时,我会得到一个要签名的字符串Php 将amazon MWS草稿行查询转换为API调用,php,xml,api,amazon,amazon-mws,Php,Xml,Api,Amazon,Amazon Mws,我想知道是否有办法将我的Amazon MWS查询转换为API调用 e、 g.当使用MWS草稿行时,我会得到一个要签名的字符串 "mws.amazonservices.co.uk" ."/Products/2011-10-01" ."AWSAccessKeyId=xxx&Action=ListMatchingProducts" ."&MarketplaceId=xxx&Query=star%20wars&SellerId=xxx" ."
"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
在花了几天的时间试图开始工作之后,我放弃了,一直希望下面的函数能够返回一个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
}
有人有什么建议吗?您需要实际调用API。您正在使用的字符串没有指定URL的http://部分。我也一直在努力解决这个问题,下面是我如何为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);
}
?>
AWS\u访问\u密钥\u ID,
“操作”=>“列表匹配产品”,
'SellerId'=>商户ID,
“SignatureMethod”=>“HmacSHA256”,
'SignatureVersion'=>“2”,
'Timestamp'=>gmdate(“Y-m-d\TH:i:s.\\0\\0\\0\\Z”,time()),
“版本”=>“2011-10-01”,
“MarketplaceId”=>市场ID,
“查询”=>$searchTerm,
'QueryContextId'=>“图书”);
//对URL参数进行排序
$url_parts=array();
foreach(数组_键($params)作为$key)
$url\u parts[]=$key。"=" . str_replace('%7E','~',rawurlencode($params[$key]);
排序($url\u部分);
//构造要签名的字符串
$url\u string=内爆(&,$url\u部分);
$string_to_sign=“GET\nmws.amazonservices.com\n/Products/2011-10-01\n”$url_字符串;
//签署请求
$signature=hash_hmac(“sha256”,$string_to_sign,AWS_SECRET_ACCESS_KEY,TRUE);
//Base64对签名进行编码并使其URL安全
$signature=urlencode(base64_encode($signature));
$url=”https://mws.amazonservices.com/Products/2011-10-01" . '?' . $url\u字符串。“&Signature=”$签名;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_超时,15);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
$response=curl\u exec($ch);
$parsed_xml=simplexml_load_string($response);
返回($parsed_xml);
}
?>
.inc.config.php
文件包含我的访问密钥、密钥等
编辑:$searchterm
是我在表单中传递的isbn。我在使用此代码时遇到了一些问题,在上载到托管的web服务器时,它可以正常工作,但在使用windows和xampp运行本地时却不能
如果遇到问题,请尝试将其添加到curl setopt块的末尾
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0)
顺便说一句,您还需要修改xampp文件夹中的php.ini文件以启用curl。因此,当实际使用http或https时,您在$resp中得到了什么结果?另外,如果您尝试在浏览器中或通过其他方法(如cURL、wget等)调用这些URL,$resp只返回错误,但在浏览器中,返回的XML字符串可能是XML格式不正确,或者可能有CDATA块,在这种情况下,您需要使用LIBXML\u NOCDATA选项。这看起来很有趣,我来试一试,我所需要做的就是获取我以前使用的配置文件,我获取了新API附带的配置文件。函数没有返回任何内容吗?我还需要更改什么吗?您需要更改一些参数,以满足您所使用的API的需要。你可以通过在所有信息中硬编码和使用print\r($parsed\u xml)来测试它,这将向你展示xml。没问题,就像我说的,我也为此奋斗了很长一段时间,其他人也帮了我。祝你好运,Amazon文档非常缺乏。