kuser / sp-api-sdk
亚马逊销售合作伙伴API - PHP SDK
Requires
- php: ~8.1 || ~8.2
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- psr/http-client: ^1.0.1
- psr/http-factory: ^1.0
- psr/log: ^1.1||^2.0||^3.0
Requires (Dev)
- kriswallsmith/buzz: ^1.2
- monolog/monolog: ^3.3
- nyholm/psr7: ^1.4
- symfony/dotenv: ^6.1
- symfony/var-dumper: ^6.1
- dev-master
- 5.x-dev
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.x-dev
- 4.0.13.x-dev
- 4.0.13
- 4.0.12
- 4.0.11
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.x-dev
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.x-dev
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.x-dev
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/composer/tools/phpstan/phpstan-1.10.27
- dev-dependabot/composer/tools/rector/rector-0.17.10
- dev-dependabot/composer/tools/vimeo/psalm-5.14.1
- dev-dependabot/composer/symfony/var-dumper-6.3.3
- dev-dependabot/composer/tools/friendsofphp/php-cs-fixer-3.22.0
- dev-dependabot/composer/tools/phpunit/phpunit-9.6.10
This package is auto-updated.
Last update: 2024-09-10 18:49:11 UTC
README
此仓库不是亚马逊官方的PHP SP API库。
为什么选择这个库?
此SDK的主要目标是提供符合亚马逊审计要求的亚马逊SP API SDK。
亚马逊审计可能会发生在必须访问包含PII的API端点的系统中。
目前已有几个PHP SP API SDK可用于PHP,但其中大多数都存在许多自动生成代码的问题。
- 硬编码的依赖项,如
guzzlehttp/guzzle
或aws/aws-sdk-php
- 遗留代码库(7.2)
- 没有日志记录器
- SDK以单个卖家为中心,不适合大型系统
- 缺少或缺乏对
client_credentials
授权类型的支持 - 未覆盖所有API
- 没有扩展
此库的目标是解决上述所有问题。
安装
composer require amazon-php/sp-api-sdk "^4.0"
此库尚未处于稳定状态,请谨慎使用。
版本发布
版本已弃用,因为试图通过在Open API规范中使用“标签”来更清楚地了解亚马逊正在做的事情。这次尝试失败了,为了保持向后兼容性承诺,必须在2.x中引入类名更改。1.0版本将不再更新,请迁移到2.0版本,该版本将与Amazon Models的分支3.x保持一致。直到旧模型不再存在,2.x和3.x分支应并行维护。
4.x带来了以下亚马逊API模型的BC breaks
- 商品列表
- 报告
- 供应商
- 直接配送运输
- 直接配送订单
- 直接配送交易
可用的SDK
SellingPartnerSDK - 所有SDK的外观
- 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
授权
为了开始使用SP API,您首先需要注册为开发者并创建应用程序。整个过程在亚马逊官方指南中有描述。
亚马逊建议在创建应用程序时使用IAM角色,但这需要额外的API请求来获取访问令牌。使用用户IAM更简单,只需确保用户具有以下内联策略。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "execute-api:Invoke",
"Resource": "arn:aws:execute-api:*:*:*"
}
]
}
IAM用户
将刷新令牌转换为访问令牌的示例。
<?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角色
<?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');
开发
该库中的99%的代码是从亚马逊销售合作伙伴API模型使用OpenAPI Generator工具自动生成的。输出随后由RectorPHP自动升级到PHP 7.4版本,最终编码标准也由PHP CS Fixer自动统一。
要求
为了重新生成代码(例如当API定义更改时),请执行以下代码
composer generate
示例
<?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()); }
日志记录
默认日志级别设置为DEBUG,但可以在配置中将其更改为其他任何级别,以用于所有API中的所有操作,或者仅为特定API中的特定操作。
$configuration->setDefaultLogLevel(\Psr\Log\LogLevel::INFO);
也可以排除特定API或特定操作进行日志记录(例如包含PII或敏感数据的API)。
$configuration->setLogLevel(CatalogItemSDK::API_NAME, CatalogItemSDK::OPERATION_GETCATALOGITEM, LogLevel::INFO);
$configuration->setSkipLogging(TokensSDK::API_NAME);
$configuration->setSkipLogging(AuthorizationSDK::API_NAME, AuthorizationSDK::OPERATION_GETAUTHORIZATIONCODE);
最后,您还可以在日志记录http请求/响应时忽略特定头信息。默认情况下,配置已设置为忽略以下敏感授权头信息
'authorization',
'x-amz-access-token',
'x-amz-security-token',
'proxy-authorization',
'www-authenticate',
'proxy-authenticate',
您还可以添加自己的忽略头信息
$configuration->loggingAddSkippedHeader('some-sensitive-key');
扩展
每个SDK都允许您注册自定义扩展,这些扩展在发送API请求之前和之后执行。
<?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"; } });
沙盒
可以使用配置打开沙盒模式
$configuration->setSandbox();
一些API端点在功能测试中进行了覆盖。要运行使用沙盒模式的测试,您需要创建.env
文件并填写您的凭据
cp .env.dist .env
然后您可以通过执行composer test:functional
来进入沙盒测试套件。