From 24e388494705f97801b5cecb2e56ea2613ae5d34 Mon Sep 17 00:00:00 2001 From: Joe Dawson Date: Fri, 11 Dec 2015 19:31:50 +0000 Subject: [PATCH] Choice of response and updating docs. --- README.md | 34 +++++++++++++++++++++++++++------- src/AmazonECS.php | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4b98354..74f5b15 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ -# Amazon ECS for Laravel +# Amazon ECS (E-Commerce Services) Package for Laravel -Whilst working on a project, I needed the ability to search for products on Amazon as well as lookup individual products based on their ASIN which led me to make a dedicated package for that as *sometimes*, the process to authorise with Amazon is a little awkward. To say the least. +If you need the ability to search Amazon's catalog of products or lookup an individual item with Laravel, then this may be the package for you. + +**Please note, you'll need to ensure you have an associate tag before using this package.** ## Installation @@ -8,12 +10,14 @@ Whilst working on a project, I needed the ability to search for products on Amaz composer require dawson/amazon-ecs ``` -After you have successfully installed, add the follow Service Provider and Facade to your `config/app.php`. +After you have successfully installed, add the follow Service Provider to your `config/app.php`. ```php Dawson\AmazonECS\AmazonECSServiceProvider::class, ``` +And the following facade, also in `config/app.php`. + ```php 'Amazon' => Dawson\AmazonECS\AmazonECSFacade::class ``` @@ -50,10 +54,10 @@ Currently, there are two methods available which are `search` and `lookup`. ### Search ```php -$response = Amazon::search('Home Alone'); +$results = Amazon::search('Home Alone')->json(); ``` -**It's that simple!** The request should return an XML response. +**It's that simple!** *Please note*, this currently searches the entire Amazon catalog. I plan on adding the ability to search within a given category *soon* so keep an eye out for that. @@ -63,11 +67,27 @@ $response = Amazon::search('Home Alone'); You can also lookup any given item, assuming it's availble on your configure locale and is a valid **ASIN**, of which is possible by doing the following: ```php -$product = Amazon::lookup('B004VLKY8M'); +$product = Amazon::lookup('B004VLKY8M')->json(); ``` This will simply return the product, it's attributes and item links. +## Responses + +Currently, there are two available response methods. The default `xml` method, or my preferred - `json`. + +The following returns an XML string. + +``` +$xml = Amazon::search('Call of Duty')->xml(); +``` + +And as you can probably assume, the following returns a JSON string. + +``` +$json = Amazon::search('Halo')->json(); +``` + ## Questions & Issues Should you have any questions or come across a problem, please feel free to submit an issue. @@ -84,4 +104,4 @@ This package is open-sourced software licensed under the MIT license. - [X] Locales - [ ] Better Exception Handling - [ ] Cart abilities, such as modifying, adding, clearing etc. -- [ ] XML to JSON (the ECS API returns an XML response, I myself would like to have the ability to convert this to JSON - but optional) +- [X] XML to JSON (the ECS API returns an XML response, I myself would like to have the ability to convert this to JSON - but optional) diff --git a/src/AmazonECS.php b/src/AmazonECS.php index 37ebc33..cb35f06 100644 --- a/src/AmazonECS.php +++ b/src/AmazonECS.php @@ -34,6 +34,8 @@ public function __construct() { $this->validConfig(); + $this->response = null; + $this->access_key = config('amazon.access_key'); $this->secret_key = config('amazon.secret_key'); $this->associate_tag = config('amazon.associate_tag'); @@ -56,7 +58,8 @@ public function search($query) $url = $this->url($params, $signature); try { - return $this->client->request('GET', $url); + $this->response = $this->client->get($url)->getBody(); + return $this; } catch(ClientException $e) { return $e->getResponse(); } @@ -76,12 +79,37 @@ public function lookup($id) $url = $this->url($params, $signature); try { - return $this->client->request('GET', $url); + $this->response = $this->client->get($url)->getBody(); + return $this; } catch(ClientException $e) { return $e->getResponse(); } } + /** + * Returns the response as XML + * + * @return Response + */ + public function xml() + { + return simplexml_load_string($this->response); + } + + /** + * Returns the response as JSON + * + * @return Response + */ + public function json() + { + $xml = simplexml_load_string($this->response); + $json = json_encode($xml); + $json = json_decode($json, true); + + return $json; + } + /** * Determines if the configuration was valid. *