Note that there are some explanatory texts on larger screens.

plurals
  1. POObtaining information from a returned array - Confusion
    primarykey
    data
    text
    <p>Afternoon! I've looked around everywhere for an answer to this, I'm attempting to build a site for a company, and they would like a form of check to their external server. I have the class that checks for this information, and have confirmed it works by using print_r(), however I'm looking for something a lot more specific from the class - just to clarify, I am very new to PHP, and do not know a heck of a lot.</p> <p>This is the class:</p> <pre><code>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 */ const STATISTIC = 0x00; const HANDSHAKE = 0x09; private $Socket; private $Players; private $Info; public function Connect( $Ip, $Port = 25565, $Timeout = 3 ) { if( !is_int( $Timeout ) || $Timeout &lt; 0 ) { throw new InvalidArgumentException( 'Timeout must be an integer.' ); } $this-&gt;Socket = @FSockOpen( 'udp://' . $Ip, (int)$Port, $ErrNo, $ErrStr, $Timeout ); if( $ErrNo || $this-&gt;Socket === false ) { throw new MinecraftQueryException( 'Could not create socket: ' . $ErrStr ); } Stream_Set_Timeout( $this-&gt;Socket, $Timeout ); Stream_Set_Blocking( $this-&gt;Socket, true ); try { $Challenge = $this-&gt;GetChallenge( ); $this-&gt;GetStatus( $Challenge ); } // We catch this because we want to close the socket, not very elegant catch( MinecraftQueryException $e ) { FClose( $this-&gt;Socket ); throw new MinecraftQueryException( $e-&gt;getMessage( ) ); } FClose( $this-&gt;Socket ); } public function GetInfo( ) { return isset( $this-&gt;Info ) ? $this-&gt;Info : false; } public function GetPlayers( ) { return isset( $this-&gt;Players ) ? $this-&gt;Players : false; } private function GetChallenge( ) { $Data = $this-&gt;WriteData( self :: HANDSHAKE ); if( $Data === false ) { throw new MinecraftQueryException( "Failed to receive challenge." ); } return Pack( 'N', $Data ); } private function GetStatus( $Challenge ) { $Data = $this-&gt;WriteData( self :: STATISTIC, $Challenge . Pack( 'c*', 0x00, 0x00, 0x00, 0x00 ) ); if( !$Data ) { throw new MinecraftQueryException( "Failed to receive status." ); } $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' =&gt; 'HostName', 'gametype' =&gt; 'GameType', 'version' =&gt; 'Version', 'plugins' =&gt; 'Plugins', 'map' =&gt; 'Map', 'numplayers' =&gt; 'Players', 'maxplayers' =&gt; 'MaxPlayers', 'hostport' =&gt; 'HostPort', 'hostip' =&gt; 'HostIp' ); foreach( $Data as $Key =&gt; $Value ) { if( ~$Key &amp; 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-&gt;Info = $Info; if( $Players ) { $this-&gt;Players = Explode( "\x00", $Players ); } } private function WriteData( $Command, $Append = "" ) { $Command = Pack( 'c*', 0xFE, 0xFD, $Command, 0x01, 0x02, 0x03, 0x04 ) . $Append; $Length = StrLen( $Command ); if( $Length !== FWrite( $this-&gt;Socket, $Command, $Length ) ) { throw new MinecraftQueryException( "Failed to write on socket." ); } $Data = FRead( $this-&gt;Socket, 2048 ); if( $Data === false ) { throw new MinecraftQueryException( "Failed to read from socket." ); } if( StrLen( $Data ) &lt; 5 || $Data[ 0 ] != $Command[ 2 ] ) { return false; } return SubStr( $Data, 5 ); } } </code></pre> <p>I am trying to get data from the GetInfo() function, I have done some googling around about returning and functions, and tried some things: <pre><code> $Query = new MinecraftQuery( ); try { $Query-&gt;Connect( 'ipgoeshere', 25565 ); $Info = $Query-&gt;GetInfo(); $maxPlayers = $Info['maxplayers']; echo $maxPlayers; } catch( MinecraftQueryException $e ) { echo $e-&gt;getMessage( ); } </code></pre> <p>Am I doing something wrong here? Or is this the completely wrong type of method. A solution would be fantastic!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload