Jump to content

Minecraft Query


To175

Recommended Posts

If I use this code, I have this error :

Failed to receive challenge.

CODE :

<?php
   require __DIR__ . '/MinecraftQuery.class.php';
   $Query = new MinecraftQuery( );
   try
   {
    $Query->Connect( 'localhost', 25565 );
    print_r( $Query->GetInfo( ) );
    print_r( $Query->GetPlayers( ) );
   }
   catch( MinecraftQueryException $e )
   {
    echo $e->getMessage( );
   }
?>

With this class :

<?php
class MinecraftQueryException extends Exception
{
// Exception thrown by MinecraftQuery class
}
class MinecraftQuery
{
/*
 * Class written by xPaw
 *
 * Website: http://xpaw.ru
 * GitHub: https://github.com/xPaw/PHP-Minecraft-Query
 */
private $Socket;
private $Challenge;
private $Players;
private $Info;
public function Connect( $Ip = 'serveurmecraft.tk', $Port = 10252, $Timeout = 3 )
{
 if( $this->Socket = FSockOpen( 'udp://' . $Ip, (int)$Port ) )
 {
  Socket_Set_TimeOut( $this->Socket, $Timeout );
  if( !$this->GetChallenge( ) )
  {
   FClose( $this->Socket );
   throw new MinecraftQueryException( "Failed to receive challenge." );
  }
  if( !$this->GetStatus( ) )
  {
   FClose( $this->Socket );
   throw new MinecraftQueryException( "Failed to receive status." );
  }
  FClose( $this->Socket );
 }
 else
 {
  throw new MinecraftQueryException( "Can't open connection." );
 }
}
public function GetInfo( )
{
 return isset( $this->Info ) ? $this->Info : false;
}
public function GetPlayers( )
{
 return isset( $this->Players ) ? $this->Players : false;
}
private function GetChallenge( )
{
 $Data = $this->WriteData( "\x09" );
 if( !$Data )
 {
  return false;
 }
 $this->Challenge = Pack( 'N', $Data );
 return true;
}
private function GetStatus( )
{
 $Data = $this->WriteData( "\x00", $this->Challenge . "\x01\x02\x03\x04" );
 if( !$Data )
 {
  return false;
 }
 $Last = "";
 $Info = Array( );
 $Data    = SubStr( $Data, 11 ); // splitnum + 2 int
 $Data    = Explode( "\x00\x00\x01player_\x00\x00", $Data );
 $Players = SubStr( $Data[ 1 ], 0, -2 );
 $Data    = Explode( "\x00", $Data[ 0 ] );
 // Array with known keys in order to validate the result
 // It can happen that server sends custom strings containing bad things (who can know!)
 $Keys = Array(
  'hostname'   => 'HostName',
  'gametype'   => 'GameType',
  'version'    => 'Version',
  'map'	    => 'Map',
  'numplayers' => 'Players',
  'maxplayers' => 'MaxPlayers',
  'hostport'   => 'HostPort',
  'hostip'	 => 'HostIp'
 );
 foreach( $Data as $Key => $Value )
 {
  if( ~$Key & 1 )
  {
   if( !Array_Key_Exists( $Value, $Keys ) )
   {
 $Last = false;
 continue;
   }
   $Last = $Keys[ $Value ];
   $Info[ $Last ] = "";
  }
  else if( $Last != false )
  {
   $Info[ $Last ] = $Value;
  }
 }
 // Ints
 $Info[ 'Players' ]    = IntVal( $Info[ 'Players' ] );
 $Info[ 'MaxPlayers' ] = IntVal( $Info[ 'MaxPlayers' ] );
 $Info[ 'HostPort' ]   = IntVal( $Info[ 'HostPort' ] );
 // Parse "plugins", if any
 if( $Info[ 'Plugins' ] )
 {
  $Data = Explode( ": ", $Info[ 'Plugins' ], 2 );
  $Info[ 'RawPlugins' ] = $Info[ 'Plugins' ];
  $Info[ 'Software' ]   = $Data[ 0 ];
  if( Count( $Data ) == 2 )
  {
   $Info[ 'Plugins' ] = Explode( "; ", $Data[ 1 ] );
  }
 }
 else
 {
  $Info[ 'Software' ] = 'Vanilla';
 }
 $this->Info = $Info;
 if( $Players )
 {
  $this->Players = Explode( "\x00", $Players );
 }
 return true;
}
private function WriteData( $Command, $Append = "" )
{
 $Command = "\xFE\xFD" . $Command . "\x01\x02\x03\x04" . $Append;
 $Length  = StrLen( $Command );
 if( $Length !== FWrite( $this->Socket, $Command, $Length ) )
 {
  return false;
 }
 $Data = FRead( $this->Socket, 1440 );
 if( StrLen( $Data ) < 5 || $Data[ 0 ] != $Command[ 2 ] )
 {
  return false;
 }
 return SubStr( $Data, 5 );
}
}

Link to comment
Share on other sites

If I use this code, I have this error : Failed to receive challenge. CODE :
 Connect( 'localhost', 25565 ); print_r( $Query->GetInfo( ) ); print_r( $Query->GetPlayers( ) ); } catch( MinecraftQueryException $e ) { echo $e->getMessage( ); } ?> 

 

I see that you are trying to connect to localhost.

Shouldn't this be the address of your minecraft server?

Link to comment
Share on other sites

Replace the localhost with the IP or domain of your game server. IIRC from when you had the port opened, your game server is not at that .tk domain.

 

Also, this thing requires rcon. Is the query option in your config enabled and does the query port match?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...