PHP code example of tiagoandre / tiktok-business-api-sdk

1. Go to this page and download the library: Download tiagoandre/tiktok-business-api-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tiagoandre / tiktok-business-api-sdk example snippets




use Tiagoandrepro\TiktokBusinessApiSdk\Http\Client;
use Tiagoandrepro\TiktokBusinessApiSdk\Auth\OAuth;

$httpClient = new Client();

// Valores dinâmicos
$appId = 'SEU_APP_ID';
$clientSecret = 'SEU_APP_SECRET';
$redirectUrl = 'https://seu_dominio.com/callback';

// Instancia a classe OAuth com os valores dinâmicos
$oauth = new OAuth($httpClient, $appId, $clientSecret, $redirectUrl);

// Gera a URL de autorização dinamicamente
$authorizationUrl = $oauth->getAuthorizationUrl('random_state_string');



use Tiagoandrepro\TiktokBusinessApiSdk\Http\Client;
use Tiagoandrepro\TiktokBusinessApiSdk\Auth\OAuth;

$httpClient = new Client();

// Valores dinâmicos
$appId = 'SEU_APP_ID';
$clientSecret = 'SEU_APP_SECRET';
$redirectUrl = 'https://seu_dominio.com/callback';

$oauth = new OAuth($httpClient, $appId, $clientSecret, $redirectUrl);

// Receber o código de autorização que foi enviado pelo TikTok após a autenticação
$code = $_GET['code'] ?? null;

if ($code) {
    $accessTokenData = $oauth->getAccessToken($code);
}

use Tiagoandrepro\TiktokBusinessApiSdk\Http\Request;
use Tiagoandrepro\TiktokBusinessApiSdk\Http\Client;
use Tiagoandrepro\TiktokBusinessApiSdk\Exceptions\HttpException;

$httpClient = new Client();

public function getAccessToken(string $code): \Tiagoandrepro\TiktokBusinessApiSdk\Http\Response
{
    try {
        $request = new Request('POST', 'oauth2/access_token', [], [
            'app_id' => $this->appId,
            'secret' => $this->clientSecret,
            'auth_code' => $code,
            'grant_type' => 'authorization_code',
        ]);

        $response = $this->httpClient->request($request);

        if (isset($data['code']) && $data['code'] !== 0) {
            throw new HttpException("TikTok API Error: {$data['message']}");
        }

        return $response;
    } catch (\Exception $e) {
        throw new HttpException("Failed to retrieve access token", $e->getCode(), $e);
    }
}