Skip to content

Servers Request

Ben Sherred edited this page Dec 5, 2019 · 5 revisions

If you want to get the server information, you can call the servers() method on the Client instance, as shown below:

<?php

require_once('vendor/autoload.php');

$client = new TruckersMP\APIClient\Client();

$servers = $client->servers()->get();

This will return an instance of ServerCollection.

The Collection

The ServerCollection returns an array of Server models. You can loop through the servers like so:

<?php

require_once('vendor/autoload.php');

$client = new TruckersMP\APIClient\Client();
$servers = $client->servers()->get();

foreach ($servers as $server) {
    // $ban is an instance of a `Server` model
}

The Model

To get the data from the model, a number of helpful getters have been created as shown below.

Method Type Description
getId() int The ID given to the server
getGame() string What game the server is for
getIp() string The server ip address
getPort() int The port that the server runs on
getName() string The name of the server
getShortName() string The short name for the server
getIdPrefix() ?string This is shown in-game in front of a player's ID
isOnline() bool If the server is online or not
getPlayers() int How many players are currently on the server
getQueue() int The number of players waiting in the queue to join the server
getMaxPlayers() int The max amount of players allowed on the server at once
getDisplayOrder() int Determines the order in which servers are displayed
hasSpeedLimit() bool If the speed limiter is enabled on the server (110 kmh for ETS2 and 80 mph for ATS)
hasCollisions() bool If server wide collisions is enabled
canPlayersHaveCars() bool If cars are enabled for players
canPlayersHavePoliceCars() bool If police cars can be driven by players
isAfkEnabled() bool If AFK kick is enabled for players
isEvent() bool If the server is an event server
isSpecialEvent() bool If the server hosts special event files (e.g. map edits, special cargos or new paint jobs)
hasPromods() bool If the server hosts ProMods
getSyncDelay() int The server tick rate

Example

Below is an example of how you would get the total number of online players:

<?php

require_once('vendor/autoload.php');

$client = new TruckersMP\APIClient\Client();
$servers = $client->servers()->get();

$onlinePlayers = 0;

foreach ($servers as $server) {
    $onlinePlayers += $server->getPlayers();
}

echo "{$onlinePlayers} truckers are driving now.";

Clone this wiki locally