Foreach Response отображает только последнее значение массива информации

Я пробовал этот код с помощью 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();
}
}}

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();
}
}
}

Надеюсь это поможет!

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector