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 = 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();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.