ecomphp / tiktokshop-php
PHP版的非官方Tiktok Shop API客户端
v2.5.4
2024-09-18 07:53 UTC
Requires
- php: ^7.2|^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.3|^6.5
Requires (Dev)
- phpunit/phpunit: ^8|^9
This package is auto-updated.
Last update: 2024-09-18 07:53:57 UTC
README
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();
- 创建认证请求
$_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);
- 在应用授权后返回到
重定向回调URL
时获取认证码,并将其交换为访问token
$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(); // 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_token
和shop_cipher
才能开始使用TiktokShop API
$client = new Client($app_key, $app_secret); $client->setAccessToken($access_token); $client->setShopCipher($shop_cipher);
- 获取产品列表: API文档
$products = $client->Product->getProductList([ 'page_size' => 50, ]);
- 获取订单列表: API文档
$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());