ecomphp/tiktokshop-php

PHP版的非官方Tiktok Shop API客户端

资助包维护!

v2.5.4 2024-09-18 07:53 UTC

README

Total Downloads Latest Stable Version Latest Unstable Version Build Status License

Tiktok Shop API客户端是Tiktok Shop API的简单SDK实现。

从v2.x版本开始,库使用API版本202309及更高版本。对于旧版本API,请使用v1.x版本

安装

使用Composer安装

composer require ecomphp/tiktokshop-php

配置TiktokShop PHP客户端

use EcomPHP\TiktokShop\Client;

$app_key = 'your app key';
$app_secret = 'your app secret';

$client = new Client($app_key, $app_secret);

授权token

有一个Auth类可以帮助您使用OAuth从商店获取token。

$auth = $client->auth();
  1. 创建认证请求
$_SESSION['state'] = $state = str_random(40); // random string
$auth->createAuthRequest($state);

如果您想函数返回认证url而不是自动跳转,可以将参数$return(第二个参数)设置为true。

$authUrl = $auth->createAuthRequest($state, true);

// redirect user to auth url
header('Location: '.$authUrl);
  1. 在应用授权后返回到重定向回调URL时获取认证码,并将其交换为访问token
$authorization_code = $_GET['code'];
$token = $auth->getToken($authorization_code);

$access_token = $token['access_token'];
$refresh_token = $token['refresh_token'];
  1. 获取授权商店密钥
$access_token = $token['access_token'];
$client->setAccessToken($access_token);

$authorizedShopList = $client->Authorization->getAuthorizedShop();

// extract shop_id & cipher from $authorizedShopList for use later

刷新访问token

访问token即将过期,因此您需要通过使用refresh_token来刷新新的token

$new_token = $auth->refreshNewToken($refresh_token);

$new_access_token = $new_token['access_token'];
$new_refresh_token = $new_token['refresh_token'];

API使用示例

您需要access_tokenshop_cipher才能开始使用TiktokShop API

$client = new Client($app_key, $app_secret);
$client->setAccessToken($access_token);
$client->setShopCipher($shop_cipher);
$products = $client->Product->getProductList([
    'page_size' => 50,
]);
$orders = $client->Order->getOrderList([
    'order_status' => 100, // Unpaid order
    'page_size' => 50,
]);

更改API版本

默认情况下,API版本202309将在每个API调用中使用。使用以下示例来更改它

$products = $client->Product->useVersion('202312')->checkListingPrerequisites(); 

Webhook

使用webhook接收来自Tiktok Shop的传入通知

$webhook = $client->webhook();

或手动配置webhook接收器

use EcomPHP\TiktokShop\Webhook;
use EcomPHP\TiktokShop\Errors\TiktokShopException;

$webhook = new Webhook($client);
try {
    $webhook->verify();
    $webhook->capture($_POST);
} catch (TiktokShopException $e) {
    echo "webhook error: " . $e->getMessage() . "\n";
}
echo "Type: " . $webhook->getType() . "\n";
echo "Timestamp: " . $webhook->getTimestamp() . "\n";
echo "Shop ID: " . $webhook->getShopId() . "\n";
echo "Data: \n"; // data is array
print_r($webhook->getData());