2017-02-22 73 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);