Skip to content

Commit 71bcc64

Browse files
authored
Merge pull request #5 from MarcL/fix/tidy-up
Add sort into ItemSearch and tidy up
2 parents ad9c87d + 33b79cf commit 71bcc64

File tree

5 files changed

+228
-88
lines changed

5 files changed

+228
-88
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
test.php
1+
# Composer specific
2+
composer.phar
3+
/vendor/
4+
5+
# Avoid commiting secret keys
6+
secretKeys.php

AmazonAPI.php

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AmazonAPI
1212
private $m_amazonUrl = '';
1313
private $m_locale = 'uk';
1414
private $m_retrieveArray = false;
15-
private $m_useSSL = false;
15+
private $m_useSSL = true;
1616

1717
// AWS endpoint for each locale
1818
private $m_localeTable = array(
@@ -38,7 +38,45 @@ class AmazonAPI
3838

3939
// Valid names that can be used for search
4040
private $mValidSearchNames = array(
41-
'All','Apparel','Appliances','Automotive','Baby','Beauty','Blended','Books','Classical','DVD','Electronics','Grocery','HealthPersonalCare','HomeGarden','HomeImprovement','Jewelry','KindleStore','Kitchen','Lighting','Marketplace','MP3Downloads','Music','MusicTracks','MusicalInstruments','OfficeProducts','OutdoorLiving','Outlet','PetSupplies','PCHardware','Shoes','Software','SoftwareVideoGames','SportingGoods','Tools','Toys','VHS','Video','VideoGames','Watches',
41+
'All',
42+
'Apparel',
43+
'Appliances',
44+
'Automotive',
45+
'Baby',
46+
'Beauty',
47+
'Blended',
48+
'Books',
49+
'Classical',
50+
'DVD',
51+
'Electronics',
52+
'Grocery',
53+
'HealthPersonalCare',
54+
'HomeGarden',
55+
'HomeImprovement',
56+
'Jewelry',
57+
'KindleStore',
58+
'Kitchen',
59+
'Lighting',
60+
'Marketplace',
61+
'MP3Downloads',
62+
'Music',
63+
'MusicTracks',
64+
'MusicalInstruments',
65+
'OfficeProducts',
66+
'OutdoorLiving',
67+
'Outlet',
68+
'PetSupplies',
69+
'PCHardware',
70+
'Shoes',
71+
'Software',
72+
'SoftwareVideoGames',
73+
'SportingGoods',
74+
'Tools',
75+
'Toys',
76+
'VHS',
77+
'Video',
78+
'VideoGames',
79+
'Watches'
4280
);
4381

4482
private $mErrors = array();
@@ -163,12 +201,12 @@ private function MakeRequest( $url )
163201
*
164202
* @param keywords Keywords which we're requesting
165203
* @param searchIndex Name of search index (category) requested. NULL if searching all.
166-
* @param sortBySalesRank True if sorting by sales rank, false otherwise.
204+
* @param sortBy Category to sort by, only used if searchIndex is not 'All'
167205
* @param condition Condition of item. Valid conditions : Used, Collectible, Refurbished, All
168206
*
169207
* @return mixed SimpleXML object, array of data or false if failure.
170208
*/
171-
public function ItemSearch( $keywords, $searchIndex = NULL, $sortBySalesRank = true, $condition = 'New' )
209+
public function ItemSearch( $keywords, $searchIndex = NULL, $sortBy = NULL, $condition = 'New' )
172210
{
173211
// Set the values for some of the parameters.
174212
$operation = "ItemSearch";
@@ -192,9 +230,9 @@ public function ItemSearch( $keywords, $searchIndex = NULL, $sortBySalesRank = t
192230
// Searching for specific index
193231
$request .= "&SearchIndex=" . $searchIndex;
194232

195-
// If we're sorting by sales rank
196-
if ( $sortBySalesRank && ( $searchIndex != 'All' ) )
197-
$request .= "&Sort=salesrank";
233+
// Set sort category
234+
if ( $sortBy && ( $searchIndex != 'All' ) )
235+
$request .= "&Sort=" . $sortBy;
198236
}
199237

200238
// Need to sign the request now

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2016 Marc Littlemore
3+
Copyright (c) 2017 Marc Littlemore
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 146 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,50 @@ It also assumes that you have some knowledge of Amazon's Product API and have se
88
## Installation
99
Clone the git repository:
1010

11-
git clone https://github.com/MarcL/AmazonProductAPI.git
11+
```shell
12+
git clone https://github.com/MarcL/AmazonProductAPI.git
13+
```
1214

15+
## Examples
16+
17+
I've added some simple examples in `examples.php`. To run them create a file called `secretKeys.php` containing your secret keys:
18+
19+
```php
20+
<?php
21+
$keyId = 'YOUR-AWS-KEY';
22+
$secretKey = 'YOUR-AWS-SECRET-KEY';
23+
$associateId = 'YOUR-AMAZON-ASSOCIATE-ID';
24+
?>
25+
```
26+
27+
and then run the examples with:
28+
29+
```shell
30+
php examples.php
31+
```
1332

1433
## Usage
15-
Include the library in your code
34+
Include the library in your code:
1635

17-
require_once( 'AmazonAPI' )
36+
```php
37+
include_once('./AmazonAPI.php');
38+
```
1839

19-
Instantiate the class using your secret keys
40+
Instantiate the class using your secret keys:
2041

21-
$keyId = 'YOUR-AWS-KEY';
22-
$secretKey = 'YOUR-AWS-SECRET-KEY';
23-
$associateId = 'YOUR-AMAZON-ASSOCIATE-ID';
42+
```php
43+
// Keep these safe
44+
$keyId = 'YOUR-AWS-KEY';
45+
$secretKey = 'YOUR-AWS-SECRET-KEY';
46+
$associateId = 'YOUR-AMAZON-ASSOCIATE-ID';
2447

25-
$amazonAPI = new AmazonAPI( $keyId, $secretKey, $associateId );
48+
$amazonAPI = new AmazonAPI($keyId, $secretKey, $associateId);
49+
```
50+
51+
**Note:** Keep your Amazon keys safe. Either use environment variables or include from a file that you don't check into GitHub.
2652

2753
It supports all Amazon regions:
54+
2855
* Canada ('ca')
2956
* China ('cn')
3057
* Germany ('de')
@@ -35,101 +62,141 @@ It supports all Amazon regions:
3562
* United Kingdom ('uk')
3663
* United States ('us').
3764

38-
The default is UK but to set the locale call SetLocale __before__ calling the product methods. E.g.
65+
The default is UK but to set the locale call `SetLocale()` __before__ calling the product methods. E.g.
3966

40-
$amazonAPI->SetLocale( 'us' );
67+
```php
68+
$amazonAPI->SetLocale('us');
69+
```
4170

42-
By default it will use HTTP but if you need to use SSL then call the following before using the product methods and it will connect to the HTTPS endpoints:
71+
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:
4372

44-
$amazonAPI->SetSSL( true );
73+
```
74+
$amazonAPI->SetSSL(false);
75+
```
76+
77+
**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.
4578

4679
### Item Search
4780
To search for an item use the ItemSearch method:
4881

49-
// Search for Harry Potter items in all categories
50-
$items = $amazonAPI->ItemSearch( 'harry potter' );
82+
```php
83+
// Search for harry potter items in all categories
84+
$items = $amazonAPI->ItemSearch('harry potter');
85+
86+
// Search for harry potter items in Books category only
87+
$items = $amazonAPI->ItemSearch('harry potter', 'Books');
88+
```
5189

52-
// Search for Harry Potter items in Books category only
53-
$items = $amazonAPI->ItemSearch( 'harry potter', 'Books' );
90+
#### Default sort
5491

55-
To determine valid categories for search call GetValidSearchNames() :
92+
By default, the `ItemSearch` method will search by featured. If you want to sort by another category then pass a 3rd parameter with the name of the category you wish to sort by. These differ by category type but the two you'll probably need are `price` (sort by price low to high) or `-price` (sort by price high to low). See [ItemSearch Sort Values](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/APPNDX_SortValuesArticle.html) for more details.
5693

57-
// Get an array of valid search categories we can use
58-
$searchCategories = $amazonAPI->GetValidSearchNames();
94+
```php
95+
// Search for harry potter items in Books category, sort by low to high
96+
$items = $amazonAPI->ItemSearch('harry potter', 'Books', 'price');
97+
98+
// Search for harry potter items in Books category, sort by high to low
99+
$items = $amazonAPI->ItemSearch('harry potter', 'Books', '-price');
100+
```
101+
102+
To determine valid categories for search call `GetValidSearchNames()`:
103+
104+
```php
105+
// Get an array of valid search categories we can use
106+
$searchCategories = $amazonAPI->GetValidSearchNames();
107+
```
59108

60109
### Item Lookup
61110
To look up product using the product ASIN number use ItemLookup:
62111

63-
// Retrieve specific item by id
64-
$items = $amazonAPI->ItemLookUp( 'B003U6I396' );
112+
```php
113+
// Retrieve specific item by id
114+
$items = $amazonAPI->ItemLookUp('B003U6I396');
65115

66-
// Retrieve a list of items by ids
67-
$asinIds = array( 'B003U6I396', 'B003U6I397', 'B003U6I398' );
68-
$items = $amazonAPI->ItemLookUp( $asinIds );
116+
// Retrieve a list of items by ids
117+
$asinIds = array('B003U6I396', 'B003U6I397', 'B003U6I398');
118+
$items = $amazonAPI->ItemLookUp($asinIds);
119+
```
69120

70121
## Returned data
71-
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:
122+
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:
72123

73-
// Return XML data
74-
$amazonAPI = new AmazonAPI( $keyId, $secretKey, $associateId );
75-
$items = $amazonAPI->ItemSearch( 'harry potter' );
76-
var_dump( $items );
124+
```php
125+
// Return XML data
126+
$amazonAPI = new AmazonAPI($keyId, $secretKey, $associateId);
127+
$items = $amazonAPI->ItemSearch('harry potter');
128+
var_dump($items);
129+
```
77130

78131
This will output:
79132

80-
class SimpleXMLElement#2 (2) {
81-
public $OperationRequest =>
82-
class SimpleXMLElement#3 (3) {
83-
public $RequestId =>
84-
string(36) "de58449e-0c1a-47ac-9823-00fd049c52df"
85-
public $Arguments =>
86-
class SimpleXMLElement#5 (1) {
87-
public $Argument =>
88-
array(11) {
89-
...
90-
91-
// Return simplified data
92-
$amazonAPI = new AmazonAPI( $keyId, $secretKey, $associateId );
93-
$amazonAPI->SetRetrieveAsArray();
94-
$items = $amazonAPI->ItemSearch( 'harry potter' );
95-
var_dump( $items );
96-
97-
Returning simplified data gives:
98-
99-
array(10) {
100-
[0] =>
101-
array(8) {
102-
'asin' =>
103-
string(10) "B00543R3WG"
104-
'url' =>
105-
string(212) "http://www.amazon.co.uk/Harry-Potter-Complete-8-Film-Collection/dp/B00543R3WG%3FSubscriptionId%3D1BM0B8TXM1YSZ1M0XDR2%26tag%3Ddjcr-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00543R3WG"
106-
'rrp' =>
107-
double(44.99)
108-
'title' =>
109-
string(58) "Harry Potter - The Complete 8-Film Collection [DVD] [2011]"
110-
'lowestPrice' =>
111-
double(23.4)
112-
'largeImage' =>
113-
string(53) "http://ecx.images-amazon.com/images/I/51qa9nTUsEL.jpg"
114-
'mediumImage' =>
115-
string(61) "http://ecx.images-amazon.com/images/I/51qa9nTUsEL._SL160_.jpg"
116-
'smallImage' =>
117-
string(60) "http://ecx.images-amazon.com/images/I/51qa9nTUsEL._SL75_.jpg"
118-
}
119-
[1] =>
120-
array(8) {
121-
'asin' =>
122-
string(10) "0747558191"
123-
'url' =>
124-
string(212) "http://www.amazon.co.uk/Harry-Potter-Philosophers-Stone-Rowling/dp/0747558191%3FSubscriptionId%3D1BM0B8TXM1YSZ1M0XDR2%26tag%3Ddjcr-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0747558191"
125-
'rrp' =>
126-
double(6.99)
127-
'title' =>
128-
string(40) "Harry Potter and the Philosopher\'s Stone"
129-
133+
```shell
134+
class SimpleXMLElement#2 (2) {
135+
public $OperationRequest =>
136+
class SimpleXMLElement#3 (3) {
137+
public $RequestId =>
138+
string(36) "de58449e-0c1a-47ac-9823-00fd049c52df"
139+
public $Arguments =>
140+
class SimpleXMLElement#5 (1) {
141+
public $Argument =>
142+
array(11) {
143+
...
144+
```
145+
146+
```php
147+
// Return simplified data
148+
$amazonAPI = new AmazonAPI($keyId, $secretKey, $associateId);
149+
$amazonAPI->SetRetrieveAsArray();
150+
$items = $amazonAPI->ItemSearch('harry potter');
151+
var_dump($items);
152+
```
153+
154+
Returning simplified data gives a PHP array
155+
156+
```
157+
array(10) {
158+
[0] =>
159+
array(8) {
160+
'asin' =>
161+
string(10) "B00543R3WG"
162+
'url' =>
163+
string(212) "http://www.amazon.co.uk/Harry-Potter-Complete-8-Film-Collection/dp/B00543R3WG%3FSubscriptionId%3D1BM0B8TXM1YSZ1M0XDR2%26tag%3Ddjcr-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00543R3WG"
164+
'rrp' =>
165+
double(44.99)
166+
'title' =>
167+
string(58) "Harry Potter - The Complete 8-Film Collection [DVD] [2011]"
168+
'lowestPrice' =>
169+
double(23.4)
170+
'largeImage' =>
171+
string(53) "http://ecx.images-amazon.com/images/I/51qa9nTUsEL.jpg"
172+
'mediumImage' =>
173+
string(61) "http://ecx.images-amazon.com/images/I/51qa9nTUsEL._SL160_.jpg"
174+
'smallImage' =>
175+
string(60) "http://ecx.images-amazon.com/images/I/51qa9nTUsEL._SL75_.jpg"
176+
}
177+
[1] =>
178+
array(8) {
179+
'asin' =>
180+
string(10) "0747558191"
181+
'url' =>
182+
string(212) "http://www.amazon.co.uk/Harry-Potter-Philosophers-Stone-Rowling/dp/0747558191%3FSubscriptionId%3D1BM0B8TXM1YSZ1M0XDR2%26tag%3Ddjcr-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0747558191"
183+
'rrp' =>
184+
double(6.99)
185+
'title' =>
186+
string(40) "Harry Potter and the Philosopher\'s Stone"
187+
188+
```
130189
131190
## TODO
191+
132192
* Need to make the simplified data less hardcoded!
193+
* Make this a Composer package
194+
* Add unit tests
133195
134196
## Thanks
197+
135198
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).
199+
200+
## LICENSE
201+
202+
See [LICENSE](LICENSE)

0 commit comments

Comments
 (0)