Foreach Response 只显示最后一个数组信息的值

Foreach Response only display last value of an array information

我一直在使用 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();
    }
}}

您只收到最后一个响应的原因是您的调用不在 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();
        }
    }
}

希望对您有所帮助!