Advertisement
YoFuzzy3

MinecraftQuerySimple.class.php

Mar 20th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2.     /*
  3.      * Queries Minecraft server
  4.      * Returns array on both success and failure but with different information
  5.      *
  6.      * Written by xPaw
  7.      * Modified by YoFuzzy3
  8.      *
  9.      * Website: http://xpaw.ru
  10.      * GitHub: https://github.com/xPaw/PHP-Minecraft-Query
  11.      */
  12.  
  13. class MinecraftQuerySimple{
  14.    
  15.     public function QueryMinecraft($IP, $Port, $Timeout){
  16.         $Socket = Socket_Create(AF_INET, SOCK_STREAM, SOL_TCP);
  17.        
  18.         Socket_Set_Option($Socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => (int) $Timeout, 'usec' => 0));
  19.         Socket_Set_Option($Socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => (int) $Timeout, 'usec' => 0));
  20.        
  21.         if($Socket === FALSE || @Socket_Connect($Socket, $IP, (int) $Port) === FALSE){
  22.                 return Array(
  23.                         'Status'     => 'Offline',
  24.                         'MOTD'       => '???',
  25.                         'Players'    => 0,
  26.                         'MaxPlayers' => 0,
  27.                         'Protocol'   => 0,
  28.                         'Version'    => '???'
  29.             );
  30.         }
  31.        
  32.         Socket_Send($Socket, "\xFE\x01", 2, 0);
  33.         $Len = Socket_Recv($Socket, $Data, 512, 0);
  34.         Socket_Close($Socket);
  35.        
  36.         if($Len < 4 || $Data[0] !== "\xFF"){
  37.                 return Array(
  38.                         'Status'     => 'Offline',
  39.                         'MOTD'       => '???',
  40.                         'Players'    => 0,
  41.                         'MaxPlayers' => 0,
  42.                         'Protocol'   => 0,
  43.                         'Version'    => '???'
  44.             );
  45.         }
  46.        
  47.         $Data = SubStr($Data, 3);
  48.         $Data = iconv('UTF-16BE', 'UTF-8', $Data);
  49.        
  50.         if($Data[1] === "\xA7" && $Data[2] === "\x31"){
  51.             $Data = Explode("\x00", $Data);
  52.             return Array(
  53.                         'Status'     => 'Online',
  54.                 'MOTD'       => $Data[3],
  55.                 'Players'    => IntVal($Data[4]),
  56.                 'MaxPlayers' => IntVal($Data[5]),
  57.                 'Protocol'   => IntVal($Data[1]),
  58.                 'Version'    => $Data[2]
  59.             );
  60.         }
  61.        
  62.         $Data = Explode("\xA7", $Data);
  63.        
  64.         return Array(
  65.                 'Status'     => 'Online',
  66.             'MOTD'       => SubStr($Data[0], 0, -1),
  67.             'Players'    => isset($Data[1]) ? IntVal($Data[1]) : 0,
  68.             'MaxPlayers' => isset($Data[2]) ? IntVal($Data[2]) : 0,
  69.             'Protocol'   => 0,
  70.             'Version'    => '1.3'
  71.         );
  72.     }
  73. }
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement