Skip to content

Commit

Permalink
Choice of response and updating docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
joedawson committed Dec 11, 2015
1 parent 44f4f1d commit 24e3884
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
34 changes: 27 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# 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

```
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
```
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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)
32 changes: 30 additions & 2 deletions src/AmazonECS.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
}
Expand All @@ -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.
*
Expand Down

0 comments on commit 24e3884

Please sign in to comment.