gpt4 book ai didi

php - 使用 Amazon API 获取产品详细信息

转载 作者:可可西里 更新时间:2023-11-01 00:38:38 26 4
gpt4 key购买 nike

我有以下代码来输出亚马逊的商品列表,但我不确定如何访问特定产品(使用 Summery、评论等)。任何帮助将不胜感激。

<?php



function makeAWSUrl($parameters, $associate_tag, $access_key, $secret_key, $aws_version = '2009-06-01') {



$host = 'ecs.amazonaws.com';

$path = '/onca/xml';



$query = array(

'Service' => 'AWSECommerceService',

'AWSAccessKeyId' => $access_key,

'AssociateTag' => $associate_tag,

'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),

'Version' => $aws_version,

);



// Merge in any options that were passed in

if (is_array($parameters)) {

$query = array_merge($query, $parameters);

}



// Do a case-insensitive, natural order sort on the array keys.

ksort($query);



// create the signable string

$temp = array();



foreach ($query as $k => $v) {

$temp[] = str_replace('%7E', '~', rawurlencode($k)) . '=' . str_replace('%7E', '~', rawurlencode($v));

}



$signable = implode('&', $temp);



$stringToSign = "GET\n$host\n$path\n$signable";



// Hash the AWS secret key and generate a signature for the request.



$hex_str = hash_hmac('sha256', $stringToSign, $secret_key);



$raw = '';



for ($i = 0; $i < strlen($hex_str); $i += 2) {

$raw .= chr(hexdec(substr($hex_str, $i, 2)));

}



$query['Signature'] = base64_encode($raw);

ksort($query);



$temp = array();



foreach ($query as $k => $v) {

$temp[] = rawurlencode($k) . '=' . rawurlencode($v);

}



$final = implode('&', $temp);



return 'http://' . $host . $path . '?' . $final;

}



$url = makeAWSUrl(array('Keywords' => 'ipod',

'Operation' => 'ItemSearch',

'SearchIndex' => 'Electronics'),

'ResponseGroup' => 'Medium',

'someid', 'aaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaaaaaaaaaaa');





$response = simplexml_load_file($url);



foreach ($response->Items->Item as $item)

{

$Title [] = $item->ItemAttributes->Title;

}





foreach($Title as $CurrentTitle)

{

echo "<h2>".$CurrentTitle."</h2>";

}







?>

最佳答案

$response->Items->Item 列表包含与您的查询匹配的所有项目/文章。这是一个对象的列表。每个对象都有类似 ItemAtributes 的属性,这些属性又可以有属性。

看看 documentation查看哪些属性可用。例如 ItemAttributes->ListPrice->Amount 包含项目的价格。

例如,要为每个结果输出价格和标题,请将代码更改为

$response = simplexml_load_file($url);
foreach ($response->Items->Item as $item) {
echo "<h2>".$item->ItemAttributes->Title."</h2>";
echo "Price: ".$item->ItemAttributes->ListPrice->Amount;
}

关于php - 使用 Amazon API 获取产品详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1586086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com