How can PHP be used to close or stop a Minecraft server?

To close or stop a Minecraft server using PHP, you can create a script that sends a stop command to the server's console. This can be achieved by using PHP's socket functions to establish a connection to the Minecraft server's console port and sending the appropriate command to stop the server.

<?php
$serverAddress = '127.0.0.1'; // Minecraft server IP address
$serverPort = 25575; // Minecraft server console port

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    die("Error creating socket");
}

$result = socket_connect($socket, $serverAddress, $serverPort);
if ($result === false) {
    die("Error connecting to server");
}

$command = "stop\n"; // Command to stop the Minecraft server
socket_write($socket, $command, strlen($command));

socket_close($socket);
?>