mehr-it / sp-api-sdk
Amazon Selling Partner API - PHP SDK
Installs: 1 799
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.4 || ~8.0 || ~8.1
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- psr/http-client: ^1.0.1
- psr/http-factory: ^1.0
- psr/log: ^1.1
- symfony/polyfill-uuid: ^1.23
Requires (Dev)
- kriswallsmith/buzz: ^1.2
- monolog/monolog: ^2.2
- nyholm/psr7: ^1.4
- symfony/var-dumper: ^5.2
This package is not auto-updated.
Last update: 2024-11-19 21:09:03 UTC
README
This repository is not an official Amazon PHP library for their SP API.
Installations
composer install mehr-it/sp-api-sdk
This library is not in a stable stage yet, please use with caution.
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
- ExternalFulfillmentSDK
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 MehrIt\AmazonSellingPartner\OAuth;
use MehrIt\AmazonSellingPartner\Configuration;
use MehrIt\AmazonSellingPartner\HttpFactory;
use Buzz\Client\Curl;
use Nyholm\Psr7\Factory\Psr17Factory;
$factory = new Psr17Factory();
$client = new Curl($factory);
$oauth = new OAuth(
$client,
$httpFactory = new HttpFactory($factory, $factory),
$config = Configuration::forIAMUser(
'lwaClientId',
'lwaClientIdSecret',
'awsAccessKey',
'awsSecretKey'
)
);
$accessToken = $oauth->exchangeRefreshToken('seller_oauth_refresh_token');
IAM Role
<?php
use MehrIt\AmazonSellingPartner\OAuth;
use MehrIt\AmazonSellingPartner\Configuration;
use MehrIt\AmazonSellingPartner\HttpFactory;
use Buzz\Client\Curl;
use Nyholm\Psr7\Factory\Psr17Factory;
$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::.........'
)
)
);
$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 MehrIt\AmazonSellingPartner\Marketplace; use MehrIt\AmazonSellingPartner\Regions; use MehrIt\AmazonSellingPartner\SellingPartnerSDK; use Buzz\Client\Curl; use MehrIt\AmazonSellingPartner\Exception\ApiException; use MehrIt\AmazonSellingPartner\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, $marketplaceId = Marketplace::US()->id(), $asin = 'B07W13KJZC' ); 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 \MehrIt\AmazonSellingPartner\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"; } });