hkonnet / selling-partner-api
Amazon Selling Partner API 的 PHP 客户端
Requires
- php: >=7.0
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^6.2|^6.5|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.12
- phpunit/phpunit: ^8.0 || ^9.0
This package is not auto-updated.
Last update: 2024-09-16 21:30:11 UTC
README
一个用于连接 Amazon Selling Partner API 的 PHP 库。
赞助商
为以下公司提供动力...
如果您觉得这个库很有用,请考虑成为赞助商,或者通过下面的按钮进行一次性捐赠。我非常感激您提供的任何支持!
功能
- 支持所有 Selling Partner API 操作(针对卖家和供应商),截至 2021 年 7 月 2 日(查看此处获取所有调用文档的链接)
- 支持使用 IAM 用户和 IAM 角色ARN创建的应用程序(文档)
- 自动生成所有需要它们的受限制数据令牌的调用令牌 -- 无需额外的 Tokens API 调用
- 包括一个
Document
辅助类,用于上传和下载 feed/报告文档
安装
composer require jlevers/selling-partner-api
入门
先决条件
您需要一些东西才能开始
- Selling Partner API 开发者账户
- 配置用于与 Selling Partner API 一起使用的 AWS IAM 用户或角色
- Selling Partner API 应用程序
如果您需要有关如何设置这些内容的更多信息,请查看此博客文章。它提供了整个设置过程的详细说明。
设置
Configuration
构造函数接受单个参数:一个包含连接到 Selling Partner API 所需所有配置信息的关联数组
$config = new SellingPartnerApi\Configuration([ "lwaClientId" => "<LWA client ID>", "lwaClientSecret" => "<LWA client secret>", "lwaRefreshToken" => "<LWA refresh token>", "awsAccessKeyId" => "<AWS access key ID>", "awsSecretAccessKey" => "<AWS secret access key>", // If you're not working in the North American marketplace, change // this to another endpoint from lib/Endpoint.php "endpoint" => SellingPartnerApi\Endpoint::NA, ]);
如果您使用 IAM 角色ARN而不是用户ARN创建 Selling Partner API 应用程序,请在配置数组中传递该角色ARN
$config = new SellingPartnerApi\Configuration([ "lwaClientId" => "<LWA client ID>", "lwaClientSecret" => "<LWA client secret>", "lwaRefreshToken" => "<LWA refresh token>", "awsAccessKeyId" => "<AWS access key ID>", "awsSecretAccessKey" => "<AWS secret access key>", // If you're not working in the North American marketplace, change // this to another endpoint from lib/Endpoint.php "endpoint" => SellingPartnerApi\Endpoint::NA, "roleArn" => "<Role ARN>", ]);
存在 getter 和 setter 方法,用于配置类的 lwaClientId
、lwaClientSecret
、lwaRefreshToken
、awsAccessKeyId
、awsSecretAccessKey
和 endpoint
属性。方法名称与它们交互的属性名称一致:getLwaClientId
、setLwaClientId
、getLwaClientSecret
等。
然后将 $config
传递到任何 SellingPartnerApi\Api\*Api
类的构造函数中。请参阅 示例
部分,获取完整示例。
配置选项
传递给 Configuration
构造函数的数组接受以下键
lwaClientId (字符串)
:必需。执行 API 请求时使用的 SP API 应用程序的 LWA 客户端 ID。lwaClientSecret (字符串)
:必需。执行 API 请求时使用的 SP API 应用程序的 LWA 客户端密钥。lwaRefreshToken (字符串)
:用于执行API请求的SP API应用程序的LWA刷新令牌。必需,除非您仅使用Configuration
实例调用无需授权的操作。awsAccessKeyId (字符串)
:必需。具有SP API ExecuteAPI权限的AWS IAM用户访问密钥ID。awsSecretAccessKey (字符串)
:必需。具有SP API ExecuteAPI权限的AWS IAM用户秘密访问密钥。endpoint (数组)
:必需。包含url
键(端点URL)和region
键(AWS区域)的数组。这些数组在lib/Endpoint.php
中有预定义的常量:(NA
、EU
、FE
和NA_SANDBOX
、EU_SANDBOX
、FE_SANDBOX
。更多详情请见此处。accessToken (字符串)
:由刷新令牌生成的访问令牌。accessTokenExpiration (整数)
:与accessToken
过期时间相对应的Unix时间戳。如果提供了accessToken
,则需要accessTokenExpiration
(反之亦然)。onUpdateCredentials (可调用函数|闭包)
:当生成新的访问令牌时调用的回调函数。该函数应接受一个类型为SellingPartnerApi\Credentials
的单一参数。roleArn (字符串)
:如果您使用AWS IAM角色的ARN(而非用户ARN)设置了SP API应用程序,请在此处传递该ARN。
示例
此示例假定您有权访问Seller Insights
Selling Partner API角色,但一般格式适用于任何Selling Partner API请求。
<?php require_once(__DIR__ . '/vendor/autoload.php'); use SellingPartnerApi\Api; use SellingPartnerApi\Configuration; use SellingPartnerApi\Endpoint; $config = new Configuration([ "lwaClientId" => "amzn1.application-oa2-client.....", "lwaClientSecret" => "abcd....", "lwaRefreshToken" => "Aztr|IwEBI....", "awsAccessKeyId" => "AKIA....", "awsSecretAccessKey" => "ABCD....", // If you're not working in the North American marketplace, change // this to another endpoint from lib/Endpoint.php "endpoint" => Endpoint::NA ]); $api = new Api\SellersApi($config); try { $result = $api->getMarketplaceParticipations(); print_r($result); } catch (Exception $e) { echo 'Exception when calling SellersApi->getMarketplaceParticipations: ', $e->getMessage(), PHP_EOL; } ?>
支持的API段
卖家API
- A+ 内容API
- 授权API
- 目录API
- 旧目录API(原始目录API)
- FBA 入库API
- FBA 入库资格API
- FBA 库存API
- FBA 出库API
- 数据传输API
- 费用API
- 财务API
- 列表API
- 商家履行API
- 消息API
- 通知API
- 订单API
- 产品定价API
- 产品类型定义API
- 报告API
- 销售API
- 卖家API
- 服务API
- 装运发票API
- 运输API
- 小型轻量API
- 请求API
- 受限数据令牌API
- 上传API
供应商API
上传和下载文档
Feeds和Reports API包括涉及上传和下载文档到Amazon的操作。Amazon对所有生成的文档进行加密,并要求所有上传的文档都进行加密。SellingPartnerApi\Document
类处理所有加密/解密操作,前提是提供了一个Model\Reports\ReportDocument
、Model\Feeds\FeedDocument
或Model\Feeds\CreateFeedDocumentResponse
类的实例。这些类的实例在您调用getReportDocument
、getFeedDocument
和createFeedDocument
端点时由Amazon返回的响应中。
下载报告文档
use SellingPartnerApi\Api\ReportsApi; use SellingPartnerApi\ReportType; // Assume we've already fetched a report document ID, and that a $config object was defined above $documentId = 'foo.1234'; $reportType = ReportType::GET_FLAT_FILE_OPEN_LISTINGS_DATA; $reportsApi = new ReportsApi($config); $reportDocumentInfo = $reportsApi->getReportDocument($documentId, $reportType['name']); $docToDownload = new SellingPartnerApi\Document($reportDocumentInfo, $reportType); $contents = $docToDownload->download(); // The raw report text /* * - Array of associative arrays, (each sub array corresponds to a row of the report) if content type is ContentType::TAB or ContentType::CSV * - A nested associative array (from json_decode) if content type is ContentType::JSON * - The raw report data if content type is ContentType::PLAIN or ContentType::PDF * - PHPOffice Spreadsheet object if content type is ContentType::XLSX * - SimpleXML object if the content type is ContentType::XML */ $data = $docToDownload->getData(); // ... do something with report data
上传feed文档
use SellingPartnerApi\Api\FeedsApi; use SellingPartnerApi\FeedType; use SellingPartnerApi\Model\Feeds; $feedType = FeedType::POST_PRODUCT_PRICING_DATA; $feedsApi = new FeedsApi($config); // Create feed document $createFeedDocSpec = new Feeds\CreateFeedDocumentSpecification(['content_type' => $feedType['contentType']]); $feedDocumentInfo = $feedsApi->createFeedDocument($createFeedDocSpec); $feedDocumentId = $feedDocumentInfo->getFeedDocumentId(); // Upload feed contents to document $feedContents = file_get_contents('<your/feed/file.xml>'); $docToUpload = new SellingPartnerApi\Document($feedDocumentInfo, $feedType); $docToUpload->upload($feedContents); // ... call FeedsApi::createFeed() with $feedDocumentId
模型
大多数操作都与一个或多个模型相关联。这些模型是包含用于向API发出特定类型请求所需的数据或包含给定请求类型返回的数据的类。所有模型都具有相同的一般接口:您可以在初始化期间指定模型的所有属性,或者使用setter方法在之后设置每个属性。以下是一个使用服务API的Buyer
模型(文档,(源代码)的示例。
Buyer
模型有四个属性:buyer_id
、name
、phone
和is_prime_member
。(如果您想知道如何自己确定模型具有哪些属性,请参阅上面的docs
链接。)要创建一个设置了所有这些属性的Buyer
模型实例
$buyer = new SellingPartnerApi\Model\Service\Buyer([ "buyer_id" => "ABCDEFGHIJKLMNOPQRSTU0123456", "name" => "Jane Doe", "phone" => "+12345678901", "is_prime_member" => true ]);
或者,您可以创建一个Buyer
模型实例,然后填充其字段
$buyer = new SellingPartnerApi\Model\Service\Buyer(); $buyer->setBuyerId("ABCDEFGHIJKLMNOPQRSTU0123456"); $buyer->setName("Jane Doe"); $buyer->setPhone("+12345678901"); $buyer->setIsPrimeMember(true);
每个模型也都有您可能期望的getter方法
$buyer->getBuyerId(); // -> "ABCDEFGHIJKLMNOPQRSTU0123456" $buyer->getName(); // -> "Jane Doe" $buyer->getPhone(); // -> "+12345678901" $buyer->getIsPrimeMember(); // -> true
模型可以(并且通常确实如此)具有其他模型作为属性
$serviceJob = new SellingPartnerApi\Model\Service\Buyer([ // ... "buyer" => $buyer, // ... ]); $serviceJob->getBuyer(); // -> [Buyer instance] $serviceJob->getBuyer()->getName(); // -> "Jane Doe"