xPaw/PHP-Minecraft-Query — Issue #7: Timeout for QueryMinecraft()
Author: Endimmion
Created: 2012-02-22 12:30:06+00:00
State: open

The function to make a simple query lack a timeout function.

I use the following modifications:

function QueryMinecraft( $IP, $Port = 25565, $Timeout = 1 )
{
$Socket = Socket_Create( AF_INET, SOCK_STREAM, SOL_TCP );

socket_set_option($Socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $Timeout, 'usec' => 0));

if( $Socket === FALSE || @Socket_Connect( $Socket, $IP, (int)$Port ) === FALSE )
{
return FALSE;
}

Socket_Send( $Socket, "\xFE", 1, 0 );
$Len = Socket_Recv( $Socket, $Data, 256, 0 );
Socket_Close( $Socket );

if( $Len < 4 || $Data[ 0 ] != "\xFF" )
{
return FALSE;
}

$Data = SubStr( $Data, 3 );
$Data = iconv( 'UTF-16BE', 'UTF-8', $Data );
$Data = Explode( "\xA7", $Data );

return Array(
'HostName' => SubStr( $Data[ 0 ], 0, -1 ),
'Players' => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
'MaxPlayers' => isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0
);
}