Skip to content

Commit 6523e31

Browse files
authored
Merge pull request #7 from MarcL/feature/add-composer
Rewrite with Composer
2 parents b67245c + 58407ce commit 6523e31

17 files changed

+2221
-514
lines changed

AmazonAPI.php

Lines changed: 0 additions & 467 deletions
This file was deleted.

README.md

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# AmazonProductAPI
22
PHP library to perform product lookup and searches using the Amazon Product API.
33

4-
## Dependencies
5-
Requires the [SimpleXML](http://php.net/manual/en/book.simplexml.php) PHP and [Curl](http://php.net/manual/en/book.curl.php) extensions to be installed.
6-
It also assumes that you have some knowledge of Amazon's Product API and have set up an Amazon Associate and [Amazon Web Services](http://docs.amazonwebservices.com/AWSECommerceService/2011-08-01/GSG/GettingSetUp.html) account in order to retrieve your access keys.
7-
84
## Installation
9-
Clone the git repository:
5+
6+
This library requires the [SimpleXML](http://php.net/manual/en/book.simplexml.php) and [Curl](http://php.net/manual/en/book.curl.php) extensions to be installed and uses PHP 7+ . Installation is simple using [Composer](https://composer.io):
107

118
```shell
12-
git clone https://github.com/MarcL/AmazonProductAPI.git
9+
composer require marcl\amazonproductapi
1310
```
1411

12+
### Amazon Product API
13+
It also assumes that you have some basic knowledge of Amazon's Product API and have set up an Amazon Associate account see: [Amazon Product API Set Up](http://docs.amazonwebservices.com/AWSECommerceService/2011-08-01/GSG/GettingSetUp.html).
14+
15+
You'll need an AWS key, secret key, and associate tag. Ensure that you keep these safe!
16+
1517
## Examples
1618

1719
I've added some simple examples in `examples.php`. To run them create a file called `secretKeys.php` containing your secret keys:
@@ -30,35 +32,41 @@ and then run the examples with:
3032
php examples.php
3133
```
3234

33-
## Usage
34-
Include the library in your code:
35+
## Quick Start
36+
37+
Include the library in your code using the Composer autoloader and create an AmazonUrlBuilder with your credentials
3538

3639
```php
37-
include_once('./AmazonAPI.php');
38-
```
40+
require('vendor/autoload.php');
3941

40-
Instantiate the class using your secret keys:
42+
use MarcL\AmazonAPI;
43+
use MarcL\AmazonUrlBuilder;
4144

42-
```php
4345
// Keep these safe
4446
$keyId = 'YOUR-AWS-KEY';
4547
$secretKey = 'YOUR-AWS-SECRET-KEY';
4648
$associateId = 'YOUR-AMAZON-ASSOCIATE-ID';
4749

48-
$amazonAPI = new AmazonAPI($keyId, $secretKey, $associateId);
50+
// Setup a new instance of the AmazonUrlBuilder with your keys
51+
$urlBuilder = new AmazonUrlBuilder(
52+
$keyId,
53+
$secretKey,
54+
$associateId,
55+
'uk'
56+
);
57+
58+
// Setup a new instance of the AmazonAPI and define the type of response
59+
$amazonAPI = new AmazonAPI($urlBuilder, 'simple');
60+
61+
$items = $amazonAPI->ItemSearch('harry potter', 'Books', 'price');
62+
4963
```
5064

5165
**Note:** Keep your Amazon keys safe. Either use environment variables or include from a file that you don't check into GitHub.
5266

5367
### Locale
5468

55-
This library supports all [Product Advertising API locales](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/Locales.html) and you can set them using `SetLocale`:
56-
57-
It defaults to UK but to set the locale call `SetLocale()` __before__ calling the product methods. E.g.
58-
59-
```php
60-
$amazonAPI->SetLocale('uk');
61-
```
69+
This library supports all [Product Advertising API locales](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/Locales.html) and you can set it as you construct the AmazonUrlBuilder class with your keys.
6270

6371
At this time, these are the current supported locales:
6472

@@ -75,16 +83,6 @@ At this time, these are the current supported locales:
7583
* United Kingdom ('uk')
7684
* United States ('us')
7785

78-
### SSL
79-
80-
By default it will use HTTPS, but if you don't want to use SSL then call the following before using the product methods and it will connect to the HTTP endpoints:
81-
82-
```php
83-
$amazonAPI->SetSSL(false);
84-
```
85-
86-
**Note:** I have no idea why I originally had this method. Perhaps the Amazon Product API didn't use SSL at one point. I've enabled HTTPS as default now but you can turn it off if you need to. I assume you won't need to but I've left it in the API.
87-
8886
### Item Search
8987
To search for an item use the `ItemSearch()` method:
9088

@@ -127,12 +125,12 @@ $asinIds = array('B003U6I396', 'B003U6I397', 'B003U6I398');
127125
$items = $amazonAPI->ItemLookUp($asinIds);
128126
```
129127

130-
### Returned data
131-
By default the data will be returned as SimpleXML nodes. However if you call `SetRetrieveAsArray()` then a simplified array of items will be returned. For example:
128+
### Data Transformation
129+
By default the data will be returned as SimpleXML nodes. However, you can ask for the data to be transformed differently, depending on your use case for the API. Pass a type when instantiating the AmazonAPI class as follows:
132130

133131
```php
134-
// Return XML data
135-
$amazonAPI = new AmazonAPI($keyId, $secretKey, $associateId);
132+
// Default return type is XML
133+
$amazonAPI = new AmazonAPI($amazonUrlBuilder);
136134
$items = $amazonAPI->ItemSearch('harry potter');
137135
var_dump($items);
138136
```
@@ -154,13 +152,12 @@ class SimpleXMLElement#2 (2) {
154152
155153
```php
156154
// Return simplified data
157-
$amazonAPI = new AmazonAPI($keyId, $secretKey, $associateId);
158-
$amazonAPI->SetRetrieveAsArray();
155+
$amazonAPI = new AmazonAPI($amazonUrlBuilder, 'simple');
159156
$items = $amazonAPI->ItemSearch('harry potter');
160157
var_dump($items);
161158
```
162159
163-
Returning simplified data gives a PHP array
160+
This will return a simplified version of each item with minimal data but enough for simple use cases.
164161
165162
```
166163
array(10) {
@@ -196,16 +193,21 @@ array(10) {
196193
197194
```
198195
196+
The different data transformation types are defined as follows. Feel free to raise an issue if you'd like the data transforming to a new type.
197+
198+
* **xml** - (Default) returns data as SimpleXML nodes.
199+
* **array** - Returns data as PHP arrays and objects.
200+
* **simple** - Returns data as simplified arrays and doesn't contain all API data. Use this if you just need prices, title and images.
201+
* **json** - Returns data as a JSON string. Use this for returning from a server API endpoint.
202+
199203
## TODO
200204
201205
* Need to make the simplified data less hardcoded!
202-
* Make this a Composer package
203-
* Add unit tests
204206
205207
## Thanks
206208
207-
This library uses code based on [AWS API authentication For PHP](http://randomdrake.com/2009/07/27/amazon-aws-api-rest-authentication-for-php-5/) by [David Drake](https://github.com/randomdrake).
209+
This library uses code based on [AWS API authentication For PHP](http://randomdrake.com/2009/07/27/amazon-aws-api-rest-authentication-for-php-5/) by [David Drake](https://github.com/randomdrake) but has been mostly rewritten.
208210
209211
## LICENSE
210212
211-
See [LICENSE](LICENSE)
213+
See [LICENSE](LICENSE)

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "marcl/amazonproductapi",
3+
"description": "PHP library to perform product lookup and searches using the Amazon Product API.",
4+
"version": "3.0.0",
5+
"type": "library",
6+
"keywords": ["amazon", "product", "api"],
7+
"homepage": "https://github.com/MarcL/AmazonProductAPI/",
8+
"license": "MIT",
9+
"authors": [
10+
{
11+
"name": "Marc Littlemore",
12+
"email": "marc@marclittlemore.com",
13+
"homepage": "http://www.marclittlemore.com",
14+
"role": "Developer"
15+
}
16+
],
17+
"support": {
18+
"source": "https://github.com/MarcL/AmazonProductAPI/",
19+
"issues": "https://github.com/MarcL/AmazonProductAPI/issues"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "6.1.*"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"MarcL\\": "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"MarcL\\": "src/",
32+
"tests\\": "tests/"
33+
}
34+
}}

0 commit comments

Comments
 (0)