mehr-it/sp-api-sdk

亚马逊销售合作伙伴API - PHP SDK

2.0.1 2022-08-30 10:30 UTC

This package is not auto-updated.

Last update: 2024-09-24 20:33:24 UTC


README

此存储库不是亚马逊官方的PHP库,用于其SP API。

social-preview

安装

composer install mehr-it/sp-api-sdk

此库尚未处于稳定阶段,请谨慎使用。

可用的SDK

SellingPartnerSDK - 所有SDK的外观

授权

要开始使用SP API,您需要首先注册为开发者并创建应用程序。整个过程在亚马逊官方指南中有所描述。

亚马逊建议在创建应用程序时使用角色IAM,但这需要额外的API请求来获取访问令牌。使用用户IAM更容易,只需确保用户具有以下内联策略

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:*:*:*"
        }
    ]
}

IAM用户

将刷新令牌更改为访问令牌的示例。

<?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角色

<?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');

开发

此库中99%的代码是从亚马逊销售合作伙伴API模型自动生成的,使用了OpenAPI Generator工具。输出随后由RectorPHP自动升级到PHP 7.4版本,最后通过PHP CS Fixer自动统一编码标准。

需求

为了重新生成代码(例如,当API定义更改时),执行以下代码

composer generate

示例

<?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());
}

日志记录

默认日志级别设置为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 \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";
    }
});