Array(
[version] => Array
(
[number] => 1.0
)
[players] => Array
(
[max] => 2000
[online] => 100
))
echo "Текущая версия:" . $array['version']['number'] . ". Количество игроков " . $array['players']['online'] . '/' . $array['players']['max']; public function Query( )
{
$TimeStart = microtime(true); // for read timeout purposes
// See http://wiki.vg/Protocol (Status Ping)
$Data = "\x00"; // packet ID = 0 (varint)
$Data .= "\x04"; // Protocol version (varint)
$Data .= Pack( 'c', StrLen( $this->ServerAddress ) ) . $this->ServerAddress; // Server (varint len + UTF-8 addr)
$Data .= Pack( 'n', $this->ServerPort ); // Server port (unsigned short)
$Data .= "\x01"; // Next state: status (varint)
$Data = Pack( 'c', StrLen( $Data ) ) . $Data; // prepend length of packet ID + data
fwrite( $this->Socket, $Data ); // handshake
fwrite( $this->Socket, "\x01\x00" ); // status ping
$Length = $this->ReadVarInt( ); // full packet length
if( $Length < 10 )
{
return FALSE;
}
fgetc( $this->Socket ); // packet type, in server ping it's 0
$Length = $this->ReadVarInt( ); // string length
$Data = "";
do
{
if (microtime(true) - $TimeStart > $this->Timeout)
{
throw new MinecraftPingException( 'Server read timed out' );
}
$Remainder = $Length - StrLen( $Data );
$block = fread( $this->Socket, $Remainder ); // and finally the json string
// abort if there is no progress
if (!$block)
{
throw new MinecraftPingException( 'Server returned too few data' );
}
$Data .= $block;
} while( StrLen($Data) < $Length );
if( $Data === FALSE )
{
throw new MinecraftPingException( 'Server didn\'t return any data' );
}
$Data = JSON_Decode( $Data, true );
if( JSON_Last_Error( ) !== JSON_ERROR_NONE )
{
if( Function_Exists( 'json_last_error_msg' ) )
{
throw new MinecraftPingException( JSON_Last_Error_Msg( ) );
}
else
{
throw new MinecraftPingException( 'JSON parsing failed' );
}
return FALSE;
}
return $Data;
} use xPaw\MinecraftPing;
use xPaw\MinecraftPingException;
try
{
$Query = new MinecraftPing($ip, $port);
echo "<pre>";
print_r( $Query->Query() );
}
// catch( MinecraftPingException $e )
// {
// echo $e->getMessage();
// }
finally
{
if( $Query )
{
$Query->Close();
}
}
$q = new MinecraftPing($ip, $port);
echo "Текущая версия: " . $q->Query()['version']['number'] . '. Количество игроков: ' . $q->Query()['players']['online'] . '/' . $q->Query()['players']['max'];use xPaw\MinecraftPing; нет свойства $query $foo = "Текущая версия: "
. $data['version']['number']
. ". Количество игроков "
. $data['players']['online']
. "/"
. $data['players']['max'];$ver = $data['version']['number'];
$online = $data['players']['online'];
$max = $data['players']['max'];
$foo = "Текущая версия: $ver. Количество игроков $online/$max";