PHP code example of dev-lancer / minecraft-status

1. Go to this page and download the library: Download dev-lancer/minecraft-status 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/ */

    

dev-lancer / minecraft-status example snippets


use DevLancer\MinecraftStatus\MinecraftJavaStatus;

$status = new MinecraftJavaStatus('mc.example.com');
$result = $status->fetch()->getResult();

echo $result->motd();
echo $result->onlinePlayers() . '/' . $result->maxPlayers();

$result = (new MinecraftJavaStatus('mc.example.com'))
    ->fetch()
    ->getResult();

 declare(strict_types=1);

use DevLancer\MinecraftStatus\Exception\ConnectionException;
use DevLancer\MinecraftStatus\Exception\InvalidResponseException;
use DevLancer\MinecraftStatus\Exception\ProtocolException;
use DevLancer\MinecraftStatus\Exception\ReceiveStatusException;
use DevLancer\MinecraftStatus\Exception\TimeoutException;
use DevLancer\MinecraftStatus\MinecraftJavaStatus;

h (ConnectionException $exception) {
    echo "Connection failed: " . $exception->getMessage() . "\n";
} catch (TimeoutException $exception) {
    echo "Server read timed out: " . $exception->getMessage() . "\n";
} catch (ProtocolException | InvalidResponseException $exception) {
    echo "Invalid server response: " . $exception->getMessage() . "\n";
} catch (ReceiveStatusException $exception) {
    echo "Status could not be received: " . $exception->getMessage() . "\n";
}

raw(): array
motd(): string
onlinePlayers(): int
maxPlayers(): int

$result = $status->fetch()->getResult();

$isFull = $result->onlinePlayers() >= $result->maxPlayers();

echo $result->motd();
echo $isFull ? 'Server is full' : 'Slots are available';

foreach ($result->players as $player) {
    print_r($player);
}

if ($result->favicon !== '') {
    file_put_contents('favicon.txt', $result->favicon);
}

$status->fetch();

print_r($status->getResult()->raw());
print_r($status->getInfo());

 declare(strict_types=1);

use DevLancer\MinecraftStatus\MinecraftJavaQuery;

Result();

echo "MOTD: " . $result->motd() . "\n";
echo "Players: " . $result->onlinePlayers() . '/' . $result->maxPlayers() . "\n";
echo "Host IP: " . $result->hostIp . "\n";

foreach ($result->players as $player) {
    echo $player . "\n";
}

 declare(strict_types=1);

use DevLancer\MinecraftStatus\MinecraftBedrockStatus;

->fetch()->getResult();

echo "MOTD: " . $result->motd() . "\n";
echo "Players: " . $result->onlinePlayers() . '/' . $result->maxPlayers() . "\n";
echo "Version: " . ($result->version ?? 'unknown') . "\n";
echo "Game mode: " . ($result->gameMode ?? 'unknown') . "\n";

 declare(strict_types=1);

use DevLancer\MinecraftStatus\MinecraftJavaPreOld17Status;

)->getResult();

echo "MOTD: " . $result->motd() . "\n";
echo "Players: " . $result->onlinePlayers() . '/' . $result->maxPlayers() . "\n";
echo "Version: " . ($result->versionName ?? 'unknown') . "\n";

$status = new MinecraftJavaStatus(
    host: 'mc.example.com',
    port: 25565,
    timeout: 0.5,
    resolveSRV: true
);

$status->setTimeout(1.5);

$status->setEncoding('UTF-8');
$status->fetch();

$status = new MinecraftJavaStatus('mc.example.com', 25565, 3, false);

use DevLancer\MinecraftStatus\StatusState;

$status = new MinecraftJavaStatus('mc.example.com');

$status->status(); // StatusState::Idle

$status->fetch();
$status->status(); // StatusState::Fetched

$status->disconnect();
$status->status(); // StatusState::Fetched

use DevLancer\MinecraftStatus\Exception\ConnectionException;
use DevLancer\MinecraftStatus\Exception\InvalidResponseException;
use DevLancer\MinecraftStatus\Exception\NotConnectedException;
use DevLancer\MinecraftStatus\Exception\ProtocolException;
use DevLancer\MinecraftStatus\Exception\ReceiveStatusException;
use DevLancer\MinecraftStatus\Exception\TimeoutException;

try {
    $result = $status->fetch()->getResult();
} catch (ConnectionException $exception) {
    // DNS failed, socket could not be opened, or the endpoint refused connection.
} catch (TimeoutException $exception) {
    // The server did not answer before the configured timeout.
} catch (ProtocolException $exception) {
    // The response packet did not match the expected Minecraft protocol.
} catch (InvalidResponseException $exception) {
    // The packet was received, but the payload could not be parsed as valid status data.
} catch (ReceiveStatusException $exception) {
    // Compatibility catch for receive, timeout, protocol and invalid response failures.
} catch (NotConnectedException $exception) {
    // Data was requested before a successful fetch().
}

getInfo(): array
getResult(): StatusResultInterface
getCountPlayers(): int
getMaxPlayers(): int
getMotd(): string

$status->connect();