Skip to content

Commit

Permalink
Merge pull request #1 from shevabam/php8-support
Browse files Browse the repository at this point in the history
Add PHP 8 support
  • Loading branch information
shevabam authored Feb 13, 2024
2 parents 9d4acb8 + 635a17e commit 602a4fa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 79 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Amazon Product Information

Amazon Product Information is a PHP library for fetching simples products information from Amazon API.
Amazon Product Information is a PHP library for fetching simple products information from Amazon API.

Provides simple information about a product from its ASIN number.

Expand All @@ -17,10 +17,12 @@ The information returned is:

You have to set up an [Amazon Associates](https://affiliate-program.amazon.com/) account that has been reviewed and received final acceptance in to the Amazon Associates Program.

You'll need an access key, secret key, and partner tag *(Tools > Product Advertising API)*.
You'll need an **access key**, **secret key**, and **partner tag** *(Tools > Product Advertising API)*.

More information [here](https://webservices.amazon.com/paapi5/documentation/register-for-pa-api.html).

Amazon Product Information requires PHP 8.0+.


## Installation

Expand Down Expand Up @@ -85,10 +87,8 @@ $getResults = $Amz->searchByAsin(["B084J4MZK6", "B07ZZVWB4L"]);
```



## Full example


```php
require 'vendor/autoload.php';

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
"homepage": "https://blog.shevarezo.fr"
}
],
"version": "1.0.0",
"version": "2.0.0",
"minimum-stability": "stable",
"license": "GPL-2.0-only",
"support": {
"issues": "https://github.com/shevabam/amazon-product-info/issues",
"source": "https://github.com/shevabam/amazon-product-info/tree/master"
},
"require": {
"php": ">=5.5",
"thewirecutter/paapi5-php-sdk": "^1.0"
"php": ">=8.0",
"thewirecutter/paapi5-php-sdk": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
120 changes: 48 additions & 72 deletions src/AmazonProductInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType;
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException;
use Amazon\ProductAdvertisingAPI\v1\Configuration;
use \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Item;

class AmazonProductInfo
{
/**
* API connection data
* @var array
*/
private $params = [
private array $params = [
'access_key' => null,
'secret_key' => null,
'partner_tag' => null,
'lang' => 'us',
];

/**
* Amazon API config
* @var object
* Amazon API config=
*/
private $config;
private Configuration $config;

/**
* Amazon API instance
* @var object
*/
private $instance;

Expand All @@ -40,7 +38,7 @@ class AmazonProductInfo
* For more details refer:
* https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
*/
private $regions = [
private array $regions = [
'au' => ['host' => 'webservices.amazon.com.au', 'region' => 'us-west-2'],
'br' => ['host' => 'webservices.amazon.com.br', 'region' => 'us-east-1'],
'ca' => ['host' => 'webservices.amazon.ca', 'region' => 'us-east-1'],
Expand Down Expand Up @@ -81,7 +79,7 @@ public function __construct(array $params)
*
* @param array $params Parameters (see global var $params)
*/
private function setConfig($params)
private function setConfig(array $params): void
{
$this->config = new Configuration();

Expand All @@ -97,19 +95,15 @@ private function setConfig($params)
* Search item(s) by ASIN number
*
* @param array $itemIds Array of ASIN number
*
* @return array Products information
*/
public function searchByAsin(array $itemIds)
public function searchByAsin(array $itemIds): array
{
$results = ['error' => null, 'datas' => []];

if (count($itemIds) == 0 || !is_array($itemIds))
{
if (0 === count($itemIds) || !is_array($itemIds)) {
$results['error'] = 'No item found';
}
else
{
} else {
// https://webservices.amazon.com/paapi5/documentation/get-items.html#resources-parameter
$resources = [
GetItemsResource::ITEM_INFOTITLE,
Expand All @@ -129,57 +123,42 @@ public function searchByAsin(array $itemIds)
// Validating request
$invalidPropertyList = $getItemsRequest->listInvalidProperties();
$length = count($invalidPropertyList);
if ($length > 0)
{
if ($length > 0) {
$results['error'] = 'Error forming the request';
}
else
{
try
{
} else {
try {
$getItemsResponse = $this->instance->getItems($getItemsRequest);

if ($getItemsResponse->getItemsResult() !== null)
{
if ($getItemsResponse->getItemsResult()->getItems() !== null)
{
$responseList = $this->parseResponse($getItemsResponse->getItemsResult()->getItems());

foreach ($itemIds as $itemId)
{
$item = $responseList[$itemId];

$itemDatas = [];

if ($item !== null)
{
$itemDatas['title'] = $this->getItemTitle($item);
$itemDatas['url'] = $this->getUrl($item);
$itemDatas['images'] = $this->getImages($item);
$itemDatas['price'] = $this->getPrice($item);
}
else
{
$results['error'] = "Item not found, check errors";
}

$results['datas'][$itemId] = $itemDatas;
if (null !== $getItemsResponse->getItemsResult() && null !== $getItemsResponse->getItemsResult()->getItems()) {
$responseList = $this->parseResponse($getItemsResponse->getItemsResult()->getItems());

foreach ($itemIds as $itemId) {
$item = $responseList[$itemId];

$itemDatas = [];

if (null !== $item) {
$itemDatas['title'] = $this->getItemTitle($item);
$itemDatas['url'] = $this->getUrl($item);
$itemDatas['images'] = $this->getImages($item);
$itemDatas['price'] = $this->getPrice($item);
} else {
$results['error'] = "Item not found, check errors";
}

$results['datas'][$itemId] = $itemDatas;
}
}

if ($getItemsResponse->getErrors() !== null)
{
if (null !== $getItemsResponse->getErrors()) {
$results['error'] = $getItemsResponse->getErrors()[0]->getCode().' - '.$getItemsResponse->getErrors()[0]->getMessage();
}
} catch (Exception $exception)
{
} catch (\Exception $exception) {
$results['error'] = $exception->getMessage();
}
}
}


return $results;
}

Expand All @@ -188,15 +167,13 @@ public function searchByAsin(array $itemIds)
* Get item title
*
* @param object $item \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Item
*
* @return string Product title
*/
private function getItemTitle($item)
private function getItemTitle(Item $item): ?string
{
$result = null;

if ($item->getItemInfo() !== null && $item->getItemInfo()->getTitle() !== null && $item->getItemInfo()->getTitle()->getDisplayValue() !== null)
{
if (null !== $item->getItemInfo() && null !== $item->getItemInfo()->getTitle() && null !== $item->getItemInfo()->getTitle()->getDisplayValue()) {
$result = $item->getItemInfo()->getTitle()->getDisplayValue();
}

Expand All @@ -208,15 +185,13 @@ private function getItemTitle($item)
* Get item URL
*
* @param object $item \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Item
*
* @return string Product URL
*/
private function getUrl($item)
private function getUrl(Item $item): ?string
{
$result = null;

if ($item->getDetailPageURL() !== null)
{
if (null !== $item->getDetailPageURL()) {
$result = $item->getDetailPageURL();
}

Expand All @@ -228,18 +203,15 @@ private function getUrl($item)
* Get item images
*
* @param object $item \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Item
*
* @return array Product primary images
*/
private function getImages($item)
private function getImages(Item $item): ?array
{
$sizes = ['small', 'medium', 'large'];
$result = null;

if ($item->getImages() !== null && $item->getImages()->getPrimary() !== null)
{
foreach ($sizes as $size)
{
if (null !== $item->getImages() && null !== $item->getImages()->getPrimary()) {
foreach ($sizes as $size) {
$method = 'get'.ucfirst($size);

$result['primary'][$size] = [
Expand All @@ -249,7 +221,6 @@ private function getImages($item)
];
}
}


return $result;
}
Expand All @@ -259,15 +230,18 @@ private function getImages($item)
* Get item price
*
* @param object $item \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Item
*
* @return string Product price
*/
private function getPrice($item)
private function getPrice(Item $item): ?string
{
$result = null;

if ($item->getOffers() !== null && $item->getOffers()->getListings() !== null && $item->getOffers()->getListings()[0]->getPrice() !== null && $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount() !== null)
{
if (
null !== $item->getOffers()
&& null !== $item->getOffers()->getListings()
&& null !== $item->getOffers()->getListings()[0]->getPrice()
&& null !== $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount()
) {
$result = $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount();
}

Expand All @@ -282,12 +256,14 @@ private function getPrice($item)
* @param array $items Items value.
* @return array of \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Item mapped to ASIN.
*/
public function parseResponse($items)
public function parseResponse(array $items): array
{
$mappedResponse = [];

foreach ($items as $item) {
$mappedResponse[$item->getASIN()] = $item;
}

return $mappedResponse;
}

Expand Down

0 comments on commit 602a4fa

Please sign in to comment.