kuser/sp-api-sdk
Composer 安装命令:
composer require kuser/sp-api-sdk
包简介
Amazon Selling Partner API - PHP SDK
README 文档
README
This repository is not an official Amazon PHP library for their SP API.
Why next library?
The main goal of this SDK is to provide SDK's for the Amazon SP API in a way that would let the application to pass Amazon audit.
Amazon audit might happen to systems that must access API endpoints with PII.
There are already few php sp api SDKs available for PHP however most of them comes with many issues of auto generated code.
- hardcoded dependencies like
guzzlehttp/guzzleoraws/aws-sdk-php - legacy code base (7.2)
- no logger
- SDK's are oriented around single seller which is not suitable for bigger systems
- missing or lacking support for
client_credentialsgrant type - not all API covered
- no extensions
This library goal is to resolve all above mentioned issues.
Installations
composer require amazon-php/sp-api-sdk "^4.0"
This library is not in a stable stage yet, please use with caution.
Releases
| branch | maintained |
|---|---|
| 1.x | 🚫 |
| 2.x | 🚫 |
| 3.x | ✅ |
| 4.x | ✅ |
Version 1.x is deprecated becuase of the attempt to make a little more sense of what Amazon is doing with using "tags" in their Open API specification. This attempt failed and in order to keep Backward Compatibility promise, changes in the class names had to be introduced in 2.x. Version 1.0 is not going to be updated anymore, please migrate to version 2.0 that will stay consistent with Amazon Models Branch 3.x comes with BC breaks introduced by Amazon in Catalog Item models. Until old model won't go away, branches 2.x and 3.x should be maintained in parallel.
4.x comes with BC breaks in following Amazon api models:
- Listings
- Reports
- Vendor
- Direct Fulfillment Shipping
- Direct Fulfillment Orders
- Direct Fulfillment Transactions
Available SDKs
SellingPartnerSDK - Facade for all SDK's
- APlusSDK
- AuthorizationSDK
- CatalogItemSDK
- FBAInboundSDK
- FBAInventorySDK
- FulfillmentInboundSDK
- FBASmallAndLightSDK
- FeedsSDK
- FinancesSDK
- FulfillmentOutboundSDK
- ListingsItemsSDK
- MessagingSDK
- NotificationsSDK
- OrdersSDK
- Shipment/OrdersSDK
- ProductFeesSDK
- ProductPricingSDK
- ProductTypesDefinitionsSDK
- ReportsSDK
- SalesSDK
- SellersSDK
- ServicesSDK
- ShipmentInvoicingSDK
- ShippingSDK
- SolicitationsSDK
- TokensSDK
- UploadsSDK
- VendorSDK
Authorization
In order to start using SP API you need to first register as a Developer and create application. Whole process is described in Amazon Official Guides.
Amazon recommends to use Role IAM when creating application however this requires and additional API request in order to obtain access token. It's easier to use User IAM and just make sure that the user has following Inline Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "execute-api:Invoke",
"Resource": "arn:aws:execute-api:*:*:*"
}
]
}
IAM User
Example of changing refresh token into access token.
<?php use AmazonPHP\SellingPartner\OAuth; use AmazonPHP\SellingPartner\Configuration; use AmazonPHP\SellingPartner\HttpFactory; use Buzz\Client\Curl; use Nyholm\Psr7\Factory\Psr17Factory; use Psr\Log\NullLogger; $factory = new Psr17Factory(); $client = new Curl($factory); $oauth = new OAuth( $client, $httpFactory = new HttpFactory($factory, $factory), $config = Configuration::forIAMUser( 'lwaClientId', 'lwaClientIdSecret', 'awsAccessKey', 'awsSecretKey' ), new NullLogger() ); $accessToken = $oauth->exchangeRefreshToken('seller_oauth_refresh_token');
IAM Role
<?php use AmazonPHP\SellingPartner\OAuth; use AmazonPHP\SellingPartner\Configuration; use AmazonPHP\SellingPartner\HttpFactory; use AmazonPHP\SellingPartner\STSClient; use Buzz\Client\Curl; use Nyholm\Psr7\Factory\Psr17Factory; use Psr\Log\NullLogger; $factory = new Psr17Factory(); $client = new Curl($factory); $sts = new STSClient( $client, $requestFactory = $factory, $streamFactory = $factory ); $oauth = new OAuth( $client, $httpFactory = new HttpFactory($requestFactory, $streamFactory), $config = Configuration::forIAMRole( 'lwaClientID', 'lwaClientID', $sts->assumeRole( 'awsAccessKey', 'awsSecretKey', 'arn:aws:iam::.........' ) ), new NullLogger() ); $accessToken = $oauth->exchangeRefreshToken('seller_oauth_refresh_token');
Development
99% of code in this library is auto generated from Amazon Selling Partner API Models using OpenAPI Generator tool. Output is later automatically upgraded by RectorPHP to PHP 7.4 version and finally coding standards are also automatically unified by PHP CS Fixer.
Requirements:
In oder to regenerate code (for example when API definitions change), execute following code:
composer generate
Examples
<?php use AmazonPHP\SellingPartner\Marketplace; use AmazonPHP\SellingPartner\Regions; use AmazonPHP\SellingPartner\SellingPartnerSDK; use Buzz\Client\Curl; use AmazonPHP\SellingPartner\Exception\ApiException; use AmazonPHP\SellingPartner\Configuration; use Monolog\Handler\StreamHandler; use Monolog\Logger; use Nyholm\Psr7\Factory\Psr17Factory; require_once __DIR__ . '/vendor/autoload.php'; $factory = new Psr17Factory(); $client = new Curl($factory); $configuration = Configuration::forIAMUser( 'lwaClientId', 'lwaClientIdSecret', 'awsAccessKey', 'awsSecretKey' ); $logger = new Logger('name'); $logger->pushHandler(new StreamHandler(__DIR__ . '/sp-api-php.log', Logger::DEBUG)); $sdk = SellingPartnerSDK::create($client, $factory, $factory, $configuration, $logger); $accessToken = $sdk->oAuth()->exchangeRefreshToken('seller_oauth_refresh_token'); try { $item = $sdk->catalogItem()->getCatalogItem( $accessToken, Regions::NORTH_AMERICA, $asin = 'B07W13KJZC', $marketplaceId = [Marketplace::US()->id()] ); dump($item); } catch (ApiException $exception) { dump($exception->getMessage()); }
Logging
Default log level is set up to DEBUG, but it can be changed in configuration to any other level for all operations in all APIs or only for given operation in given API.
$configuration->setDefaultLogLevel(\Psr\Log\LogLevel::INFO);
Specific API's or only given operations can be also excluded from logging (for example APIs with PII or sensitive data).
$configuration->setLogLevel(CatalogItemSDK::API_NAME, CatalogItemSDK::OPERATION_GETCATALOGITEM, LogLevel::INFO);
$configuration->setSkipLogging(TokensSDK::API_NAME);
$configuration->setSkipLogging(AuthorizationSDK::API_NAME, AuthorizationSDK::OPERATION_GETAUTHORIZATIONCODE);
Finally, you can also ignore specific headers when logging http request/response. By default, configuration is set to ignore following sensitive authorization headers:
'authorization',
'x-amz-access-token',
'x-amz-security-token',
'proxy-authorization',
'www-authenticate',
'proxy-authenticate',
you can also add your own ignored headers:
$configuration->loggingAddSkippedHeader('some-sensitive-key');
Extensions
Each SDK allows you to register custom extensions executed before and after sending API requests.
<?php $configuration->registerExtension(new class implements \AmazonPHP\SellingPartner\Extension { public function preRequest(string $api, string $operation, RequestInterface $request): void { echo "pre: " . $api . "::" . $operation . " " . $request->getUri() . "\n"; } public function postRequest(string $api, string $operation, RequestInterface $request, ResponseInterface $response): void { echo "post: " . $api . "::" . $operation . " " . $request->getUri() . " " . $response->getStatusCode() . " rate limit: " . implode(' ', $response->getHeader('x-amzn-RateLimit-Limit')) . "\n"; } });
Sandbox
Sandbox mode can be turned on using configuration:
$configuration->setSandbox();
Some APIs endpoints are covered in functional tests.
To run tests that are using sandbox mode, you need to create .env file and populate it with your credentials:
cp .env.dist .env
Then you can enter composer test:functional te execute sandbox test suite.
kuser/sp-api-sdk 适用场景与选型建议
kuser/sp-api-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28.18k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「rest」 「api」 「amazon」 「sdk」 「selling-partner-api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kuser/sp-api-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kuser/sp-api-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kuser/sp-api-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Alfabank REST API integration
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
BlockCypher's PHP SDK for REST API
Helper classes for creating cookie headers
统计信息
- 总下载量: 28.18k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-07