Top.gg Documentation
PHP
Top.gg PHP library
PHP Library
The community-maintained PHP Library for Top.gg. If you experience any issues, please submit an issue on GitHub.
Installation
Install via composer (recommended)
composer require top-gg/php-sdk
Examples
Getting Started
This library does certain things differently from other API wrappers because it follows PHP's PSR standards. Below is sample code on how to initialize and use the API wrapper.
# Automatically load up the source library code.
include_once __DIR__ . "vendor/autoload.php";
use DBL\DBL;
$api = new DBL([
"token" => "YOUR TOP.GG API TOKEN HERE."
]);
Below are the currently accepted list of parameters when initializing the API.
Option | Type | Description | Optional? |
---|---|---|---|
token | string | The token key of the Top.gg API. | No |
auto_stats | array[] | Details for automatic stats. | Yes |
safety | bool | Webserver code protection. | Yes |
The API wrapper also presents you with a boolean instance, connected
which may be used
to check if your API token key is still valid or not.
Get statistics from Top.gg
In order to get statistics about a bot or user from Top.gg, you are able to select between
the two interchangably with the same method: find_info()
.
public function find_info(string $type, int $id): array
Argument | Type | Description |
---|---|---|
$type | string | Whether you're looking for a bot or user. |
$id | int | The bot/user ID. |
use DBL\API\Http;
if($api->connected)
{
/**
* You will always be presented two constants to choose.
*
* Http::BOT = "bots"
* Http::USER = "users"
*/
$user = $api->find_info(Http::USER, 242351388137488384);
$bot = $api->find_info(Http::BOT, 799697654279307314);
print_r($user) . print_r($bot) . PHP_EOL;
}
Search for specific bots
Similar to the same premise as using find_info()
, you are able to additionally plug in
query information that will be automatically formatted as a query-string URL search when
an HTTP request is made by the API wrapper for you. This is known as show_info()
.
public function show_info(string $type, array $json = []): array
Argument | Type | Description |
---|---|---|
$type | string | Whether you're looking for a bot or user. |
$json | array | Information to plug into the search. |
use DBL\API\Http;
if($api->connected)
{
$small_list = $api->show_info(
Http::BOT,
[
"limit" => 20,
"sort" => "-"
]
);
print_r($small_list) . PHP_EOL;
}
Checking a vote from a user
It's very important to be able and see if a user has voted for your bot. Below is the given example of how to do this.
public function get_user_vote(int $id, int $user): array
Argument | Type | Description |
---|---|---|
$id | int | The bot ID. |
$user | int | The user's snowflake ID. |
if($api->connected)
{
$isVoted = $api->get_user_vote(799697654279307314, 242351388137488384);
if($isVoted) echo "This user voted!";
}