TiktokShop API Client in PHP 技术文档
1. 安装指南
1.1 环境要求
- PHP 7.4 或更高版本
- Composer
1.2 安装步骤
- 打开终端或命令行工具。
- 运行以下命令安装 TiktokShop API Client:
composer require ecomphp/tiktokshop-php
2. 项目的使用说明
2.1 配置 TiktokShop PHP Client
在安装完成后,你需要配置你的 App Key 和 App Secret。以下是配置示例:
use EcomPHP\TiktokShop\Client;
$app_key = 'your app key';
$app_secret = 'your app secret';
$client = new Client($app_key, $app_secret);
2.2 获取授权令牌
为了使用 TiktokShop API,你需要获取授权令牌。以下是获取授权令牌的步骤:
- 创建授权请求:
$_SESSION['state'] = $state = str_random(40); // 生成随机字符串
$auth = $client->auth();
$authUrl = $auth->createAuthRequest($state, true);
// 重定向用户到授权URL
header('Location: '.$authUrl);
- 获取授权码并交换访问令牌:
$authorization_code = $_GET['code'];
$token = $auth->getToken($authorization_code);
$access_token = $token['access_token'];
$refresh_token = $token['refresh_token'];
- 获取授权商店的密钥:
$access_token = $token['access_token'];
$client->setAccessToken($access_token);
$authorizedShopList = $client->Authorization->getAuthorizedShop();
// 提取 shop_id 和 cipher 以供后续使用
2.3 刷新访问令牌
访问令牌会过期,因此你需要使用 refresh_token
刷新令牌:
$new_token = $auth->refreshNewToken($refresh_token);
$new_access_token = $new_token['access_token'];
$new_refresh_token = $new_token['refresh_token'];
3. 项目API使用文档
3.1 使用API示例
在使用 TiktokShop API 之前,你需要设置 access_token
和 shop_cipher
:
$client = new Client($app_key, $app_secret);
$client->setAccessToken($access_token);
$client->setShopCipher($shop_cipher);
3.1.1 获取产品列表
$products = $client->Product->getProductList([
'page_size' => 50,
]);
3.1.2 获取订单列表
$orders = $client->Order->getOrderList([
'order_status' => 100, // 未支付订单
'page_size' => 50,
]);
3.2 更改API版本
默认情况下,API版本为202309。你可以通过以下方式更改API版本:
$products = $client->Product->useVersion('202312')->checkListingPrerequisites();
4. 项目安装方式
4.1 通过Composer安装
使用以下命令通过Composer安装TiktokShop API Client:
composer require ecomphp/tiktokshop-php
4.2 手动配置
如果你不使用Composer,可以手动下载项目并将其包含在你的项目中。确保你已经配置了 app_key
和 app_secret
。
require 'path/to/tiktokshop-php/src/Client.php';
use EcomPHP\TiktokShop\Client;
$app_key = 'your app key';
$app_secret = 'your app secret';
$client = new Client($app_key, $app_secret);
通过以上步骤,你可以成功安装并使用 TiktokShop API Client 进行开发。