Amazon SP-API PHP 项目教程
1. 项目的目录结构及介绍
amazon-sp-api-php/
├── composer.json
├── composer.lock
├── examples/
│ ├── config.php
│ ├── getOrders.php
│ ├── getOrderItems.php
│ └── ...
├── src/
│ ├── Api/
│ │ ├── AuthorizationApi.php
│ │ ├── CatalogItemsApi.php
│ │ ├── ...
│ ├── Client/
│ │ ├── ApiClient.php
│ │ ├── Configuration.php
│ │ ├── ...
│ ├── Model/
│ │ ├── AuthorizationCode.php
│ │ ├── CatalogItem.php
│ │ ├── ...
│ └── ...
├── tests/
│ ├── Api/
│ │ ├── AuthorizationApiTest.php
│ │ ├── CatalogItemsApiTest.php
│ │ ├── ...
│ ├── Client/
│ │ ├── ApiClientTest.php
│ │ ├── ConfigurationTest.php
│ │ ├── ...
│ └── ...
└── ...
目录结构介绍
composer.json
和composer.lock
:Composer 依赖管理文件。examples/
:包含示例代码,如config.php
和getOrders.php
等。src/
:项目的源代码目录,包含 API、客户端和模型等。Api/
:包含各个 API 接口的实现。Client/
:包含客户端配置和请求处理等。Model/
:包含数据模型的定义。
tests/
:包含单元测试代码。
2. 项目的启动文件介绍
在 examples/
目录下,有多个示例文件,其中 getOrders.php
是一个典型的启动文件示例。
getOrders.php
文件介绍
<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/config.php';
use ClouSale\AmazonSellingPartnerAPI\Api\OrdersApi;
use ClouSale\AmazonSellingPartnerAPI\Configuration;
use ClouSale\AmazonSellingPartnerAPI\ApiException;
$config = new Configuration();
$config->setHost($endpoint);
$config->setAccessToken($access_token);
$config->setRegion($region);
$ordersApi = new OrdersApi($config);
try {
$result = $ordersApi->getOrders($marketplace_ids, $created_after, $created_before, $last_updated_after, $last_updated_before, $order_statuses, $fulfillment_channels, $payment_methods, $buyer_email, $seller_order_id, $max_results_per_page, $easy_ship_shipment_statuses, $next_token, $amazon_order_ids, $actual_fulfillment_supply_source_id, $is_ispu, $store_chain_store_id);
print_r($result);
} catch (ApiException $e) {
echo 'Exception when calling OrdersApi->getOrders: ', $e->getMessage(), PHP_EOL;
}
启动文件说明
- 引入必要的依赖和配置文件。
- 创建
Configuration
对象并设置相关参数。 - 实例化
OrdersApi
对象。 - 调用
getOrders
方法获取订单数据,并处理可能的异常。
3. 项目的配置文件介绍
在 examples/
目录下,config.php
是主要的配置文件。
config.php
文件介绍
<?php
$endpoint = 'https://sellingpartnerapi-na.amazon.com';
$access_token = 'your_access_token';
$region = 'us-east-1';
$marketplace_ids = ['ATVPDKIKX0DER'];
$created_after = '2023-01-01T00:00:00Z';
$created_before = '2023-12-31T23:59:59Z';
$last_updated_after = '2023-01-01T00: