thecodebunny / pa-api
TheCodeBunny\PaApi 5.0 PHP SDK
dev-main
2023-04-04 09:59 UTC
Requires
- php: ^8.0
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^6.2|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.12
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-09-04 12:59:43 UTC
README
此存储库包含允许您从PHP应用程序中访问产品广告API的开源PHP SDK。
亚马逊提供的代码副本
这是一个几乎完全相同的公开副本,即亚马逊提供的代码,因为截至撰写时,他们的版本无法通过Packagist获取。
我们没有以任何方式更改API,但我们清理了代码的部分,并已更新了依赖项。以下列出了更改内容。
从亚马逊的更改
- 将使用
\GuzzleHttp\Psr7\build_query
替换为\GuzzleHttp\Psr7\Query::build
https://github.com/thecodebunny/pa-api/pull/8 - 添加了对Guzzle 7的支持 https://github.com/thecodebunny/pa-api/pull/6
- 当使用PHP 8时,已删除弃用警告 https://github.com/thecodebunny/pa-api/pull/13
- 当使用PHP 8时,已删除动态属性创建警告 https://github.com/thecodebunny/pa-api/pull/13
- 已更新PHP最小版本为PHP 8 https://github.com/thecodebunny/pa-api/pull/13
- 代码符合PSR-2规范 https://github.com/thecodebunny/pa-api/pull/13
- 已更新开发依赖项以反映PHP 8版本要求 https://github.com/thecodebunny/pa-api/pull/13
安装
可以使用 Composer 安装产品广告API PHP SDK。该SDK在 Packagist 下的 thecodebunny/pa-api
包中可用。如果Composer已在您的系统上全局安装,您可以在项目的根目录中运行以下命令将SDK作为依赖项添加
composer require thecodebunny/pa-api
用法
注意:此版本的PHP产品广告API SDK需要PHP 5.5或更高版本。
简单的示例,用于使用关键词 'Harry Potter' 在图书类别中搜索亚马逊产品 SearchItems
<?php /** * Copyright 2023 Hemang Vyas. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * https://apache.ac.cn/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ /* * TheCodeBunny\PaApi * * https://webservices.amazon.com/paapi5/documentation/index.html */ /* * This sample code snippet is for TheCodeBunny\PaApi 5.0's SearchItems API * * For more details, refer: https://webservices.amazon.com/paapi5/documentation/search-items.html */ use TheCodeBunny\PaApi\ApiException; use TheCodeBunny\PaApi\Core\api\DefaultApi; use TheCodeBunny\PaApi\Core\PartnerType; use TheCodeBunny\PaApi\Core\ProductAdvertisingAPIClientException; use TheCodeBunny\PaApi\Core\SearchItemsRequest; use TheCodeBunny\PaApi\Core\SearchItemsResource; use TheCodeBunny\PaApi\Configuration; // require_once(__DIR__ . '/vendor/autoload.php'); // change path as needed $config = new Configuration(); /* * Add your credentials */ # Please add your access key here $config->setAccessKey('<YOUR ACCESS KEY>'); # Please add your secret key here $config->setSecretKey('<YOUR SECRET KEY>'); # Please add your partner tag (store/tracking id) here $partnerTag = '<YOUR PARTNER TAG>'; /* * 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; } ?>
完整的文档、安装说明和示例可在此处找到。
许可
此SDK根据Apache许可证版本2.0分发,有关更多信息,请参阅LICENSE.txt和NOTICE.txt。