2017-02-22 149 views
0

我想创建一个应用程序来查找多个项目。我可以使用单个变量运行代码,但只要将变量设置为数组并运行foreach循环,它就会给我rawurlencode erros。它声明第一行必须是一个字符串。有点困惑到哪里去从这里。亚马逊api php urlencode错误

x = array(744750545472, 705911706019); 

foreach ($x as $value) { 
$params = array(
"Service" => "AWSECommerceService", 
"Operation" => "ItemLookup", 
"AWSAccessKeyId" => "ACCESSKEY", 
"AssociateTag" => "[email protected]", 
"ItemId" => $x, 
"IdType" => "UPC", 
"ResponseGroup" => "ItemAttributes,OfferFull,Offers,SalesRank", 
"SearchIndex" => "All" 
); 

// Set current timestamp if not set 
if (!isset($params["Timestamp"])) { 
$params["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z'); 
} 

// Sort the parameters by key 
ksort($params); 

$pairs = array(); 

foreach ($params as $key => $value) { 
array_push($pairs, rawurlencode($key)."=".rawurlencode($value)); 
} 

// Generate the canonical query 
$canonical_query_string = join("&", $pairs); 

// Generate the string to be signed 
$string_to_sign = "GET\n".$endpoint."\n".$uri."\n".$canonical_query_string; 

// Generate the signature required by the Product Advertising API 
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, 

List item 

aws_secret_key, true)); 

// Generate the signed URL 
$request_url = 'http://'.$endpoint.$uri.'?'.$canonical_query_string.'&Signature='.rawurlencode($signature); 


$data = simplexml_load_file($request_url); 

echo $data->Items->Item->ASIN; 
+0

删除原来是否贴凭据 – C2486

+0

是我的回答有帮助吗?请参阅http://stackoverflow.com/help/someone-answers。谢谢! – miken32

回答

0

可以使用http_build_query()函数从阵列构建URL查询字符串:

$params = array(
"Service" => "AWSECommerceService", 
"Operation" => "ItemLookup", 
"AWSAccessKeyId" => "AKIAIBHBCCTSI4ZL27GA", 
"AssociateTag" => "[email protected]", 
"ItemId" => $x, 
"IdType" => "UPC", 
"ResponseGroup" => "ItemAttributes,OfferFull,Offers,SalesRank", 
"SearchIndex" => "All", 
"Timestamp" => gmdate('Y-m-d\TH:i:s\Z'), 
} 

// Sort the parameters by key 
ksort($params); 

$canonical_query_string = http_build_query($params);