Today I went looking for examples of how to talk to a minecraft server via RCON. There is not a lot out there, and that surprised me. I did eventually find a PHP script that worked, but it required the modification of an existing class. I don’t like modifying someone else’s source class files to make a program work, that is why PHP has the ability to extend the class. What follows is a simple program, that extendis the verion 1.0 rcon.class.php from http://fremnet.net/article/199/source-rcon-class and connects to a minecraft 1.2.3 server and executes the console command “say”, the result is returned in a string. I hope this example helps others out. See the example after the break.
<?php
include_once("rcon.class.php");
// Extend the rcon class to tweak it for minecraft.
class minecraftRcon extends rcon {
function mcSendCommand($Command) {
$this->_Write(SERVERDATA_EXECCOMMAND,$Command,'');
}
function mcRconCommand($Command) {
$this->mcSendcommand($Command);
$ret = $this->Read();
return $ret[$this->_Id]['S1'];
}
}
// Server connection varialbes
$server = "minecraft.example.com";
$rconPort = 123456;
$rconPass = "PASSWORD";
// Connect to the server
$r = new minecraftRcon($server, $rconPort, $rconPass);
// Authenticate, and if so, execute command(s)
if ( $r->Auth() ) {
echo "Authenticatedn";
// Send a command
var_dump($r->mcRconCommand('say Hello World!'));
}
?>
Awesome tutorial! I just caught you right ontime!
Thanks,
Max
I doesn’t seem to authenticate for me. The connection works and I see it’s trying to access it from the Minecraft console.
Proper client with bukkit color support: http://forums.bukkit.org/threads/admin-rcon-client-for-minecraft-servers-new-version-available.70910/
Just popped over to say, it’s truly awesome to see someone using something I’ve written in such a public way.
I have a same problem!
Commands aren’t run when I run the php code on page but I’ve “Rcon connection from: IP” in console!
for me, the Auth() function needed adjustment so under ‘class minecraftRcon extends rcon {‘ just paste:
function Auth () {
$PackID = $this->_Write(SERVERDATA_AUTH,$this->Password);$ret = $this->_PacketRead();
if ($ret[0][‘ID’] == -1) {die(“Authentication Failuren”);}
return true;
}