我一直在使用Amazon-product-api尝试此代码。我想检索一系列ASIN代码的信息。遗憾的是,我当前的代码只显示数组的最后一个ASIN的信息。我想在响应中显示代码数组的每个ASIN的信息。
class UniqueCode extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'code:unique';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Retrieve Information Based On Unique Code Description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$client = new GuzzleHttp\Client();
$access_key = '$accesskey';
$secret_key = '$secretkey';
$associate_tag = '$associate-tag';
$timestamp = date('c');
$uniquecodes = array("B01B0UET6O", "B01AMY8K4Q","B00JJZ7WUI", "B00JJZ7VE0","B00JJZ7URS", "B00JJZ7TME","B00JJZ7S2U", "B01FFQJ4MS","B00VB1TU44");
foreach ($uniquecodes as $codes) {
$query = [
'Service' => 'AWSECommerceService',
'Operation' => 'ItemLookup',
'ResponseGroup' => 'Large',
'IdType' => 'ASIN',
'ItemId' => $codes,
'Condition' => "New",
'AssociateTag' => $associate_tag,
'AWSAccessKeyId' => $access_key,
'Timestamp' => $timestamp
];
}
ksort($query);
$sign = http_build_query($query);
$request_method = 'GET';
$base_url = 'webservices.amazon.com';
$endpoint = '/onca/xml';
$string_to_sign = "{$request_method}\n{$base_url}\n{$endpoint}\n{$sign}";
$signature = base64_encode(
hash_hmac("sha256", $string_to_sign, $secret_key, true)
);
$query['Signature'] = $signature;
try {
$response = $client->request(
'GET', 'http://webservices.amazon.com/onca/xml',
['query' => $query]
);
$contents = new SimpleXMLElement($response->getBody()->getContents());
echo "<pre>";
print_r($contents);
echo "</pre>";
} catch(Exception $e) {
echo "something went wrong: <br>";
echo $e->getMessage();
}
}}
答案 0 :(得分:1)
您之前只收到最后一个回复的原因是因为您的呼叫不在foreach循环中。
public function handle()
{
$client = new GuzzleHttp\Client();
$access_key = '$accesskey';
$secret_key = '$secretkey';
$associate_tag = '$associate-tag';
$timestamp = date('c');
$uniquecodes = ["B01B0UET6O", "B01AMY8K4Q", "B00JJZ7WUI", "B00JJZ7VE0", "B00JJZ7URS", "B00JJZ7TME", "B00JJZ7S2U", "B01FFQJ4MS", "B00VB1TU44"];
foreach ($uniquecodes as $codes) {
$query = [
'Service' => 'AWSECommerceService',
'Operation' => 'ItemLookup',
'ResponseGroup' => 'Large',
'IdType' => 'ASIN',
'ItemId' => $codes,
'Condition' => "New",
'AssociateTag' => $associate_tag,
'AWSAccessKeyId' => $access_key,
'Timestamp' => $timestamp,
];
ksort($query);
$sign = http_build_query($query);
$request_method = 'GET';
$base_url = 'webservices.amazon.com';
$endpoint = '/onca/xml';
$string_to_sign = "{$request_method}\n{$base_url}\n{$endpoint}\n{$sign}";
$signature = base64_encode(
hash_hmac("sha256", $string_to_sign, $secret_key, true)
);
$query['Signature'] = $signature;
try {
$response = $client->request(
'GET', 'http://webservices.amazon.com/onca/xml',
['query' => $query]
);
$contents = new SimpleXMLElement($response->getBody()->getContents());
echo "<pre>";
print_r($contents);
echo "</pre>";
} catch (Exception $e) {
echo "something went wrong: <br>";
echo $e->getMessage();
}
}
}
希望这有帮助!