dongzongzhi6953 2017-02-22 01:11
浏览 105

亚马逊api php urlencode错误

i am trying to create an app to look up multiple items. I can run the code with a single variable for an item but as soon as make the variable an array and run foreach loop it is giving me rawurlencode erros. it states that the first line must be a string. Bit confused as to where to go from here.

x = array(744750545472, 705911706019);

foreach ($x as $value) {
$params = array(
"Service" => "AWSECommerceService",
"Operation" => "ItemLookup",
"AWSAccessKeyId" => "ACCESSKEY",
"AssociateTag" => "eledesmaj@example.com",
"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
".$endpoint."
".$uri."
".$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;
  • 写回答

1条回答 默认 最新

  • douyuanliao8815 2017-02-22 01:14
    关注

    You can use the http_build_query() function to build URL query strings from arrays:

    $params = array(
    "Service" => "AWSECommerceService",
    "Operation" => "ItemLookup",
    "AWSAccessKeyId" => "AKIAIBHBCCTSI4ZL27GA",
    "AssociateTag" => "eledesmaj@gmail.com",
    "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);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了