· 9 years ago · Jan 14, 2017, 07:38 PM
1class UniqueCode extends Command
2{
3 /**
4 * The name and signature of the console command.
5 *
6 * @var string
7 */
8 protected $signature = 'code:unique';
9
10 /**
11 * The console command description.
12 *
13 * @var string
14 */
15 protected $description = 'Retrieve Information Based On Unique Code Description';
16
17 /**
18 * Create a new command instance.
19 *
20 * @return void
21 */
22 public function __construct()
23 {
24 parent::__construct();
25 }
26
27 /**
28 * Execute the console command.
29 *
30 * @return mixed
31 */
32 public function handle()
33 {
34 $client = new GuzzleHttpClient();
35
36 $access_key = '$accesskey';
37 $secret_key = '$secretkey';
38 $associate_tag = '$associate-tag';
39
40 $timestamp = date('c');
41 $uniquecodes = array("B01B0UET6O", "B01AMY8K4Q","B00JJZ7WUI", "B00JJZ7VE0","B00JJZ7URS", "B00JJZ7TME","B00JJZ7S2U", "B01FFQJ4MS","B00VB1TU44");
42 foreach ($uniquecodes as $codes) {
43 $query = [
44 'Service' => 'AWSECommerceService',
45 'Operation' => 'ItemLookup',
46 'ResponseGroup' => 'Large',
47 'IdType' => 'ASIN',
48 'ItemId' => $codes,
49 'Condition' => "New",
50 'AssociateTag' => $associate_tag,
51 'AWSAccessKeyId' => $access_key,
52 'Timestamp' => $timestamp
53 ];
54 }
55 ksort($query);
56
57 $sign = http_build_query($query);
58
59 $request_method = 'GET';
60 $base_url = 'webservices.amazon.com';
61 $endpoint = '/onca/xml';
62 $string_to_sign = "{$request_method}n{$base_url}n{$endpoint}n{$sign}";
63 $signature = base64_encode(
64 hash_hmac("sha256", $string_to_sign, $secret_key, true)
65 );
66
67 $query['Signature'] = $signature;
68
69 try {
70 $response = $client->request(
71 'GET', 'http://webservices.amazon.com/onca/xml',
72 ['query' => $query]
73 );
74
75 $contents = new SimpleXMLElement($response->getBody()->getContents());
76 echo "<pre>";
77 print_r($contents);
78 echo "</pre>";
79 } catch(Exception $e) {
80 echo "something went wrong: <br>";
81 echo $e->getMessage();
82 }
83}}