PHP code example of notwonderful / trovo-sdk

1. Go to this page and download the library: Download notwonderful/trovo-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/ */

    

notwonderful / trovo-sdk example snippets


use Notwonderful\TrovoSdk\TrovoClient;
use Notwonderful\TrovoSdk\Config;
use Notwonderful\TrovoSdk\Enum\Scope;

$trovo = new TrovoClient(new Config(
    clientId: 'your_client_id',
    clientSecret: 'your_client_secret',
    redirectUri: 'https://example.com/callback',
));

// 1. Redirect user to Trovo login
$url = $trovo->getAuthorizationCodeUrl(
    scopes: [Scope::UserDetailsSelf, Scope::ChannelDetailsSelf],
    state: 'random_csrf_token',
);

// 2. After redirect, exchange code for tokens
$token = $trovo->exchangeCode($_GET['code']);
// $token->accessToken, $token->refreshToken, $token->expiresIn

// 3. Set the token for subsequent API calls
$trovo->setAccessToken($token->accessToken);

$url = $trovo->getImplicitFlowUrl(
    scopes: [Scope::UserDetailsSelf],
    state: 'random_csrf_token',
);
// Access token will be in the URL fragment after redirect

// Refresh
$newToken = $trovo->refreshToken($token->refreshToken);

// Validate
$info = $trovo->validateToken($token->accessToken);
// $info->uid, $info->scopes, $info->expireTs, $info->isExpired()

// Revoke
$trovo->revokeToken($token->accessToken);

// Top categories (sorted by popularity)
$categories = $trovo->categories()->getTopCategories();
// returns Category[] — id, name, shortName, iconUrl, desc

// Search
$result = $trovo->categories()->search(query: 'apex', limit: 10);
// $result->items (Category[]), $result->hasMore

// Top live channels with pagination
$page = $trovo->channels()->getTopChannels(limit: 20);
// $page->items (Channel[]), $page->totalPage, $page->cursor, $page->token

// Next page
$page2 = $trovo->channels()->getTopChannels(
    limit: 20,
    after: true,
    token: $page->token,
    cursor: $page->cursor,
);

// Channel info by ID or username
$channel = $trovo->channels()->getById(channelId: 100000031);
$channel = $trovo->channels()->getById(username: 'username');

// Own channel with stream key (s->totalPage

// Viewers (grouped by role)
$viewers = $trovo->channels()->getViewers(channelId: 100000031);
// $viewers['chatters']['moderators']['viewers'], $viewers['total'], ...

// Lookup by usernames
$users = $trovo->users()->getByUsernames(['user1', 'user2']);
// returns User[] — userId, userName, nickName, channelId

// Authenticated user info (

use Notwonderful\TrovoSdk\Enum\SortDirection;

// Requires channel_subscriptions scope
$subs = $trovo->subscribers()->get(
    channelId: 100000031,
    limit: 50,
    offset: 0,
    direction: SortDirection::Desc,
);
// $subs->items (Subscriber[]), $subs->total
// Subscriber: userId, username, displayName, subCreatedAt, subLv, subTier

use Notwonderful\TrovoSdk\Enum\EmoteType;

$emotes = $trovo->emotes()->get(
    emoteType: EmoteType::All,
    channelIds: [100000031],
);
// Raw array: globalEmotes, eventEmotes, customizedEmotes

// Live stream URLs (restricted access)
$urls = $trovo->streams()->getLiveStreamUrls(channelId: 100000031);
// returns StreamUrl[] — playUrl, desc (resolution)

// Past streams
use Notwonderful\TrovoSdk\Enum\Period;

$past = $trovo->streams()->getPastStreams(
    channelId: 100000031,
    period: Period::Month,
    limit: 10,
);
// $past->items (PastStream[]), $past->total, $past->totalPage

$clips = $trovo->clips()->get(
    channelId: 100000031,
    period: Period::Week,
    limit: 20,
);
// $clips->items (Clip[]) — clipId, title, url, thumbnail, duration, views, likes

// Send to own channel (yChannel('Hello!');

// Send to another channel (e message (,
    userId: '100000021',
);

// Chat commands (

use Notwonderful\TrovoSdk\Enum\FulfillmentStatus;

$drops = $trovo->drops()->getEntitlements(
    limit: 50,
    fulfillmentStatus: FulfillmentStatus::Claimed,
);
// $drops->items (DropEntitlement[]), $drops->total, $drops->hasMore

$results = $trovo->drops()->updateEntitlements(
    entitlementIds: ['ent_1', 'ent_2'],
    fulfillmentStatus: FulfillmentStatus::Fulfilled,
);
// returns DropUpdateResult[] — entitlementId, status, isSuccess()

use Notwonderful\TrovoSdk\TrovoClient;

$trovo = app(TrovoClient::class);
$categories = $trovo->categories()->getTopCategories();

public function __construct(private TrovoClient $trovo) {}

use Notwonderful\TrovoSdk\Exception\ApiException;
use Notwonderful\TrovoSdk\Exception\AuthenticationException;
use Notwonderful\TrovoSdk\Exception\RateLimitException;

try {
    $user = $trovo->users()->getUserInfo();
} catch (RateLimitException $e) {
    // $e->limit, $e->remaining, $e->resetsAt, $e->getSecondsUntilReset()
    sleep($e->getSecondsUntilReset());
} catch (AuthenticationException $e) {
    // Access token missing or not set
} catch (ApiException $e) {
    // $e->statusCode (HTTP), $e->trovoErrorCode, $e->trovoError, $e->getMessage()
}

use Notwonderful\TrovoSdk\Http\HttpClientInterface;

$trovo = new TrovoClient($config, new MyCustomHttpClient());
bash
php artisan vendor:publish --tag=trovo-config