2017-01-14 4 views
1

Amazon-product-apiを使用してこのコードを試しています。私はASINコードの配列の情報を取得したいと思います。残念ながら、私の現在のコードは配列の最後のASINの情報しか表示しません。私はレスポンスにコード配列の各ASINの情報を表示したいと思います。Foreachレスポンスは配列情報の最後の値のみを表示します

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($ uniquecodes as $ codes){' wahtはあなたが得ているエラーです。 –

+0

アレイの最後の値(B00VB1TU44)に対してxml応答を表示するだけで、各ASINコード(配列( "B01B0UET6O"、 "B01AMY8K4Q"、 "B00JJZ7WUI"、 "B00JJZ7VE0"、 "B00JJZ7VEUR" "B00JJZ7TME"、 "B00JJZ7S2U"、 "B01FFQJ4MS"、 "B00VB1TU44") –

+1

それぞれのコードをリクエストしたいのですか?(合計9個のリクエスト) –

答えて

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

+0

ありがとう、非常に簡単な私のエラー!brgds! –

関連する問題