Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thewirecutter/paapi5-php-sdk

Amazon Product Advertising API v5 PHP SDK (Amazon ships the code but doesn't provide a packagist repo)
https://github.com/thewirecutter/paapi5-php-sdk

Last synced: 3 months ago
JSON representation

Amazon Product Advertising API v5 PHP SDK (Amazon ships the code but doesn't provide a packagist repo)

Awesome Lists containing this project

README

        

# Product Advertising API 5.0 SDK for PHP

[![Version](https://img.shields.io/packagist/v/thewirecutter/paapi5-php-sdk)](https://img.shields.io/packagist/v/thewirecutter/paapi5-php-sdk)

[![Total Downloads](https://img.shields.io/packagist/dt/thewirecutter/paapi5-php-sdk.svg?style=flat)](https://packagist.org/packages/thewirecutter/paapi5-php-sdk)

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.

## Copy of Amazon's Provided Code

This is a near identical 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.

We have not changed the API in any way, however we did cleanup portions of the code and have updated dependencies. A listing of changes are provided below.

### Changes from Amazon

* Replaced usage of `\GuzzleHttp\Psr7\build_query` with `\GuzzleHttp\Psr7\Query::build` https://github.com/thewirecutter/paapi5-php-sdk/pull/8
* Added Support for Guzzle 7 https://github.com/thewirecutter/paapi5-php-sdk/pull/6
* Removed Deprecation Warnings when using PHP 8 https://github.com/thewirecutter/paapi5-php-sdk/pull/13
* Removed Dynamic Property Creation Warnings when using PHP 8 https://github.com/thewirecutter/paapi5-php-sdk/pull/13
* Updated PHP Minimum Version to PHP 8 https://github.com/thewirecutter/paapi5-php-sdk/pull/13
* CodeSniffed to PSR-2 https://github.com/thewirecutter/paapi5-php-sdk/pull/13
* Updated Dev Dependencies to reflect PHP 8 version requirement https://github.com/thewirecutter/paapi5-php-sdk/pull/13

## Installation
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:

```sh
composer require thewirecutter/paapi5-php-sdk
```

## Usage
> **Note:** This version of the Product Advertising API SDK for PHP requires PHP 5.5 or greater.

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:

```php
setAccessKey('');
# Please add your secret key here
$config->setSecretKey('');

# Please add your partner tag (store/tracking id) here
$partnerTag = '';

/*
* PAAPI host and region to which you want to send request
* For more details refer:
* https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
*/
$config->setHost('webservices.amazon.com');
$config->setRegion('us-east-1');

$apiInstance = new DefaultApi(
/*
* If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
* This is optional, `GuzzleHttp\Client` will be used as default.
*/
new GuzzleHttp\Client(), $config);

# Request initialization

# Specify keywords
$keyword = 'Harry Potter';

/*
* Specify the category in which search request is to be made
* For more details, refer:
* https://webservices.amazon.com/paapi5/documentation/use-cases/organization-of-items-on-amazon/search-index.html
*/
$searchIndex = "Books";

# Specify item count to be returned in search result
$itemCount = 1;

/*
* Choose resources you want from SearchItemsResource enum
* For more details, refer:
* https://webservices.amazon.com/paapi5/documentation/search-items.html#resources-parameter
*/
$resources = [
SearchItemsResource::ITEM_INFOTITLE,
SearchItemsResource::OFFERSLISTINGSPRICE];

# Forming the request
$searchItemsRequest = new SearchItemsRequest();
$searchItemsRequest->setSearchIndex($searchIndex);
$searchItemsRequest->setKeywords($keyword);
$searchItemsRequest->setItemCount($itemCount);
$searchItemsRequest->setPartnerTag($partnerTag);
$searchItemsRequest->setPartnerType(PartnerType::ASSOCIATES);
$searchItemsRequest->setResources($resources);

# Validating request
$invalidPropertyList = $searchItemsRequest->listInvalidProperties();
$length = count($invalidPropertyList);
if ($length > 0) {
echo "Error forming the request", PHP_EOL;
foreach ($invalidPropertyList as $invalidProperty) {
echo $invalidProperty, PHP_EOL;
}
return;
}

# Sending the request
try {
$searchItemsResponse = $apiInstance->searchItems($searchItemsRequest);

echo 'API called successfully', PHP_EOL;
echo 'Complete Response: ', $searchItemsResponse, PHP_EOL;

# Parsing the response
if ($searchItemsResponse->getSearchResult() !== null) {
echo 'Printing first item information in SearchResult:', PHP_EOL;
$item = $searchItemsResponse->getSearchResult()->getItems()[0];
if ($item !== null) {
if ($item->getASIN() !== null) {
echo "ASIN: ", $item->getASIN(), PHP_EOL;
}
if ($item->getDetailPageURL() !== null) {
echo "DetailPageURL: ", $item->getDetailPageURL(), PHP_EOL;
}
if ($item->getItemInfo() !== null
and $item->getItemInfo()->getTitle() !== null
and $item->getItemInfo()->getTitle()->getDisplayValue() !== null) {
echo "Title: ", $item->getItemInfo()->getTitle()->getDisplayValue(), PHP_EOL;
}
if ($item->getOffers() !== null
and $item->getOffers() !== null
and $item->getOffers()->getListings() !== null
and $item->getOffers()->getListings()[0]->getPrice() !== null
and $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount() !== null) {
echo "Buying price: ", $item->getOffers()->getListings()[0]->getPrice()
->getDisplayAmount(), PHP_EOL;
}
}
}
if ($searchItemsResponse->getErrors() !== null) {
echo PHP_EOL, 'Printing Errors:', PHP_EOL, 'Printing first error object from list of errors', PHP_EOL;
echo 'Error code: ', $searchItemsResponse->getErrors()[0]->getCode(), PHP_EOL;
echo 'Error message: ', $searchItemsResponse->getErrors()[0]->getMessage(), PHP_EOL;
}
} catch (ApiException $exception) {
echo "Error calling PA-API 5.0!", PHP_EOL;
echo "HTTP Status Code: ", $exception->getCode(), PHP_EOL;
echo "Error Message: ", $exception->getMessage(), PHP_EOL;
if ($exception->getResponseObject() instanceof ProductAdvertisingAPIClientException) {
$errors = $exception->getResponseObject()->getErrors();
foreach ($errors as $error) {
echo "Error Type: ", $error->getCode(), PHP_EOL;
echo "Error Message: ", $error->getMessage(), PHP_EOL;
}
} else {
echo "Error response body: ", $exception->getResponseBody(), PHP_EOL;
}
} catch (Exception $exception) {
echo "Error Message: ", $exception->getMessage(), PHP_EOL;
}
?>
```

Complete documentation, installation instructions, and examples are available [here](https://webservices.amazon.com/paapi5/documentation/index.html).

## License
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.