You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-41Lines changed: 43 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,19 @@
1
1
# AmazonProductAPI
2
2
PHP library to perform product lookup and searches using the Amazon Product API.
3
3
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
-
8
4
## 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):
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
+
15
17
## Examples
16
18
17
19
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:
30
32
php examples.php
31
33
```
32
34
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
35
38
36
39
```php
37
-
include_once('./AmazonAPI.php');
38
-
```
40
+
require('vendor/autoload.php');
39
41
40
-
Instantiate the class using your secret keys:
42
+
use MarcL\AmazonAPI;
43
+
use MarcL\AmazonUrlBuilder;
41
44
42
-
```php
43
45
// Keep these safe
44
46
$keyId = 'YOUR-AWS-KEY';
45
47
$secretKey = 'YOUR-AWS-SECRET-KEY';
46
48
$associateId = 'YOUR-AMAZON-ASSOCIATE-ID';
47
49
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');
**Note:** Keep your Amazon keys safe. Either use environment variables or include from a file that you don't check into GitHub.
52
66
53
67
### Locale
54
68
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.
62
70
63
71
At this time, these are the current supported locales:
64
72
@@ -75,16 +83,6 @@ At this time, these are the current supported locales:
75
83
* United Kingdom ('uk')
76
84
* United States ('us')
77
85
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
-
88
86
### Item Search
89
87
To search for an item use the `ItemSearch()` method:
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:
132
130
133
131
```php
134
-
// Return XML data
135
-
$amazonAPI = new AmazonAPI($keyId, $secretKey, $associateId);
132
+
// Default return type is XML
133
+
$amazonAPI = new AmazonAPI($amazonUrlBuilder);
136
134
$items = $amazonAPI->ItemSearch('harry potter');
137
135
var_dump($items);
138
136
```
@@ -154,13 +152,12 @@ class SimpleXMLElement#2 (2) {
154
152
155
153
```php
156
154
// Return simplified data
157
-
$amazonAPI = new AmazonAPI($keyId, $secretKey, $associateId);
158
-
$amazonAPI->SetRetrieveAsArray();
155
+
$amazonAPI = new AmazonAPI($amazonUrlBuilder, 'simple');
159
156
$items = $amazonAPI->ItemSearch('harry potter');
160
157
var_dump($items);
161
158
```
162
159
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.
164
161
165
162
```
166
163
array(10) {
@@ -196,16 +193,21 @@ array(10) {
196
193
…
197
194
```
198
195
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
+
199
203
## TODO
200
204
201
205
* Need to make the simplified data less hardcoded!
202
-
* Make this a Composer package
203
-
* Add unit tests
204
206
205
207
## Thanks
206
208
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.
0 commit comments