PHP code example of top-tl / toptl

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

    

top-tl / toptl example snippets




opTL\TopTL;

$client = new TopTL('your-api-token');

// Get listing info
$listing = $client->getListing('mybot');
echo $listing->title;
echo $listing->memberCount;

// Get votes
$votes = $client->getVotes('mybot');
echo $votes->votes;
echo $votes->monthlyVotes;

// Check if a user has voted
$voted = $client->hasVoted('mybot', 123456789);
echo $voted ? 'Voted' : 'Not voted';

// Post stats
$client->postStats('mybot', memberCount: 5000, groupCount: 120);

// Get global stats
$stats = $client->getStats();
echo $stats->totalListings;

use TopTL\TopTL;
use TopTL\Autoposter;

$client = new TopTL('your-api-token');

$autoposter = new Autoposter(
    client: $client,
    username: 'mybot',
    statsCallback: function () {
        // Return your current stats
        return [
            'memberCount' => getMyMemberCount(),
            'groupCount' => getMyGroupCount(),
        ];
    },
    interval: 1800, // 30 minutes (default)
);

$autoposter->onPost(function (array $result) {
    echo "Stats posted successfully\n";
});

$autoposter->onError(function (\Throwable $e) {
    echo "Error posting stats: " . $e->getMessage() . "\n";
});

// Start the loop (blocking)
$autoposter->start();

$autoposter->postOnce();