Skip to content

Commit 6ebc2be

Browse files
authored
Merge pull request #10 from thewirecutter/update-to-latest-amazon-version
Updates To the Latest Amazon Code
2 parents 11f1d82 + 1b81b6e commit 6ebc2be

File tree

103 files changed

+4663
-449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+4663
-449
lines changed

README.md

+64-52
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
# Product Advertising API SDK for PHP (v1)
2-
[![CircleCI](https://circleci.com/gh/thewirecutter/paapi5-php-sdk.svg?style=svg)](https://circleci.com/gh/thewirecutter/paapi5-php-sdk)
1+
# Product Advertising API 5.0 SDK for PHP
2+
3+
[![Version](https://img.shields.io/packagist/v/thewirecutter/paapi5-php-sdk)](https://img.shields.io/packagist/v/thewirecutter/paapi5-php-sdk)
4+
5+
[![Total Downloads](https://img.shields.io/packagist/dt/thewirecutter/paapi5-php-sdk.svg?style=flat)](https://packagist.org/packages/thewirecutter/paapi5-php-sdk)
36

47
This repository contains the open source PHP SDK that allows you to access the [Product Advertising API](https://webservices.amazon.com/paapi5/documentation/index.html) from your PHP app.
58

6-
## Installation
9+
## Copy of Amazon's Provided Code
710

8-
The Product Advertising API PHP SDK can be installed with [Composer](https://getcomposer.org/). Run this command:
11+
This is a public copy of [Amazon's provided code](https://webservices.amazon.com/paapi5/documentation/quick-start/using-sdk.html), as their version is not available through Packagist as of writing.
12+
13+
## Installation
14+
The Product Advertising API PHP SDK can be installed with [Composer](https://getcomposer.org/). The SDK is available via [Packagist](http://packagist.org/) under the [`thewirecutter/paapi5-php-sdk`](https://packagist.org/packages/thewirecutter/paapi5-php-sdk) package. If Composer is installed globally on your system, you can run the following in the base directory of your project to add the SDK as a dependency:
915

1016
```sh
11-
$ composer require thewirecutter/paapi5-php-sdk
17+
composer require thewirecutter/paapi5-php-sdk
1218
```
13-
## Usage
1419

20+
## Usage
1521
> **Note:** This version of the Product Advertising API SDK for PHP requires PHP 5.5 or greater.
1622
17-
Simple example for searching items.
23+
Simple example for [SearchItems](https://webservices.amazon.com/paapi5/documentation/search-items.html) to discover Amazon products with the keyword 'Harry Potter' in Books category:
1824

1925
```php
2026
<?php
21-
2227
/**
2328
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2429
*
@@ -39,73 +44,76 @@ Simple example for searching items.
3944
*
4045
* https://webservices.amazon.com/paapi5/documentation/index.html
4146
*/
42-
47+
4348
/*
4449
* This sample code snippet is for ProductAdvertisingAPI 5.0's SearchItems API
4550
*
4651
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/search-items.html
4752
*/
48-
49-
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\api\DefaultApi;
53+
5054
use Amazon\ProductAdvertisingAPI\v1\ApiException;
51-
use Amazon\ProductAdvertisingAPI\v1\Configuration;
52-
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsRequest;
53-
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsResource;
55+
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\api\DefaultApi;
5456
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType;
5557
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException;
56-
58+
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsRequest;
59+
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsResource;
60+
use Amazon\ProductAdvertisingAPI\v1\Configuration;
61+
5762
require_once(__DIR__ . '/vendor/autoload.php'); // change path as needed
58-
59-
63+
64+
6065
$config = new Configuration();
6166

6267
/*
6368
* Add your credentials
64-
* Please add your access key here
6569
*/
70+
# Please add your access key here
6671
$config->setAccessKey('<YOUR ACCESS KEY>');
6772
# Please add your secret key here
6873
$config->setSecretKey('<YOUR SECRET KEY>');
69-
74+
7075
# Please add your partner tag (store/tracking id) here
7176
$partnerTag = '<YOUR PARTNER TAG>';
72-
77+
7378
/*
7479
* PAAPI host and region to which you want to send request
75-
* For more details refer: https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
80+
* For more details refer:
81+
* https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
7682
*/
7783
$config->setHost('webservices.amazon.com');
7884
$config->setRegion('us-east-1');
79-
85+
8086
$apiInstance = new DefaultApi(
81-
/*
82-
* If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
83-
* This is optional, `GuzzleHttp\Client` will be used as default.
84-
*/
87+
/*
88+
* If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
89+
* This is optional, `GuzzleHttp\Client` will be used as default.
90+
*/
8591
new GuzzleHttp\Client(), $config);
86-
92+
8793
# Request initialization
88-
94+
8995
# Specify keywords
9096
$keyword = 'Harry Potter';
91-
97+
9298
/*
9399
* Specify the category in which search request is to be made
94-
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/use-cases/organization-of-items-on-amazon/search-index.html
100+
* For more details, refer:
101+
* https://webservices.amazon.com/paapi5/documentation/use-cases/organization-of-items-on-amazon/search-index.html
95102
*/
96-
$searchIndex = "All";
97-
103+
$searchIndex = "Books";
104+
98105
# Specify item count to be returned in search result
99106
$itemCount = 1;
100-
107+
101108
/*
102109
* Choose resources you want from SearchItemsResource enum
103-
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/search-items.html#resources-parameter
110+
* For more details, refer:
111+
* https://webservices.amazon.com/paapi5/documentation/search-items.html#resources-parameter
104112
*/
105-
$resources = array(
113+
$resources = [
106114
SearchItemsResource::ITEM_INFOTITLE,
107-
SearchItemsResource::OFFERSLISTINGSPRICE);
108-
115+
SearchItemsResource::OFFERSLISTINGSPRICE];
116+
109117
# Forming the request
110118
$searchItemsRequest = new SearchItemsRequest();
111119
$searchItemsRequest->setSearchIndex($searchIndex);
@@ -114,7 +122,7 @@ $searchItemsRequest->setItemCount($itemCount);
114122
$searchItemsRequest->setPartnerTag($partnerTag);
115123
$searchItemsRequest->setPartnerType(PartnerType::ASSOCIATES);
116124
$searchItemsRequest->setResources($resources);
117-
125+
118126
# Validating request
119127
$invalidPropertyList = $searchItemsRequest->listInvalidProperties();
120128
$length = count($invalidPropertyList);
@@ -134,32 +142,32 @@ try {
134142
echo 'Complete Response: ', $searchItemsResponse, PHP_EOL;
135143

136144
# Parsing the response
137-
if ($searchItemsResponse->getSearchResult() != null) {
145+
if ($searchItemsResponse->getSearchResult() !== null) {
138146
echo 'Printing first item information in SearchResult:', PHP_EOL;
139147
$item = $searchItemsResponse->getSearchResult()->getItems()[0];
140-
if ($item != null) {
141-
if ($item->getASIN() != null) {
148+
if ($item !== null) {
149+
if ($item->getASIN() !== null) {
142150
echo "ASIN: ", $item->getASIN(), PHP_EOL;
143151
}
144-
if ($item->getDetailPageURL() != null) {
152+
if ($item->getDetailPageURL() !== null) {
145153
echo "DetailPageURL: ", $item->getDetailPageURL(), PHP_EOL;
146154
}
147-
if ($item->getItemInfo() != null
148-
and $item->getItemInfo()->getTitle() != null
149-
and $item->getItemInfo()->getTitle()->getDisplayValue() != null) {
155+
if ($item->getItemInfo() !== null
156+
and $item->getItemInfo()->getTitle() !== null
157+
and $item->getItemInfo()->getTitle()->getDisplayValue() !== null) {
150158
echo "Title: ", $item->getItemInfo()->getTitle()->getDisplayValue(), PHP_EOL;
151159
}
152-
if ($item->getOffers() != null
153-
and $item->getOffers() != null
154-
and $item->getOffers()->getListings() != null
155-
and $item->getOffers()->getListings()[0]->getPrice() != null
156-
and $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount() != null) {
160+
if ($item->getOffers() !== null
161+
and $item->getOffers() !== null
162+
and $item->getOffers()->getListings() !== null
163+
and $item->getOffers()->getListings()[0]->getPrice() !== null
164+
and $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount() !== null) {
157165
echo "Buying price: ", $item->getOffers()->getListings()[0]->getPrice()
158166
->getDisplayAmount(), PHP_EOL;
159167
}
160168
}
161169
}
162-
if ($searchItemsResponse->getErrors() != null) {
170+
if ($searchItemsResponse->getErrors() !== null) {
163171
echo PHP_EOL, 'Printing Errors:', PHP_EOL, 'Printing first error object from list of errors', PHP_EOL;
164172
echo 'Error code: ', $searchItemsResponse->getErrors()[0]->getCode(), PHP_EOL;
165173
echo 'Error message: ', $searchItemsResponse->getErrors()[0]->getMessage(), PHP_EOL;
@@ -180,6 +188,10 @@ try {
180188
} catch (Exception $exception) {
181189
echo "Error Message: ", $exception->getMessage(), PHP_EOL;
182190
}
191+
?>
183192
```
184193

185-
Complete documentation, installation instructions, and examples are available [here](https://webservices.amazon.com/paapi5/documentation/with-sdk.html).
194+
Complete documentation, installation instructions, and examples are available [here](https://webservices.amazon.com/paapi5/documentation/index.html).
195+
196+
## License
197+
This SDK is distributed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0), see LICENSE.txt and NOTICE.txt for more information.

0 commit comments

Comments
 (0)