SRCDS Steam group


Query a server with PHP
#39
I've updated this script to remove those hacky things and added some more functionality. I had to invent php byte buffer as I couldn't find anything similar lol.

Anyway here it is:

Code:
class ByteBuffer
{
    public $data = array(), $pointer;
    
    function __construct($data)
    {
        $this->data = str_split($data);
        $this->pointer = 0;
    }
    
    public function ReadByte()
    {
        return ord($this->data[$this->pointer++]);
    }
    
    public function ReadShort()
    {
        return (ord($this->data[$this->pointer++]) | (ord($this->data[$this->pointer++])<<8));
    }
    
    public function ReadInt()
    {
        return (ord($this->data[$this->pointer++]) | (ord($this->data[$this->pointer++])<<8) | (ord($this->data[$this->pointer++])<<16) | (ord($this->data[$this->pointer++])<<24));
    }
    
    public function ReadChar()
    {
        return $this->data[$this->pointer++];
    }
    
    public function ReadString()
    {
        $string = '';
        while ($this->data[$this->pointer] != "\0")
        {
            $string .= $this->data[$this->pointer];
            $this->pointer++;
        }
        $this->pointer++;
        return $string;
    }
}

    function source_query($ip)
    {
        $cut = explode(":", $ip);
        $HL2_address = gethostbyname($cut[0]);
        $HL2_port = $cut[1];

        $HL2_command = "\377\377\377\377TSource Engine Query\0";
        $HL2_socket = fsockopen("udp://".$HL2_address, $HL2_port, $errno, $errstr,3);
        fwrite($HL2_socket, $HL2_command);
        $JunkHead = fread($HL2_socket,4);
        $CheckStatus = socket_get_status($HL2_socket);

        if ($CheckStatus["unread_bytes"] == 0)
        {
            return 0;
        }

        $HL2_stats = '';
        $do = 1;
        while($do)
        {
            $str = fread($HL2_socket,1);
            $HL2_stats .= $str;
            $status = socket_get_status($HL2_socket);
            if($status["unread_bytes"] == 0)
            {
                $do = 0;
            }
        }
        fclose($HL2_socket);

        $result = '';
        $x = 0;
        while ($x <= strlen($HL2_stats))
        {
            $x++;
            $result.= substr($HL2_stats, $x, 1);    
        }

        return $result;
    }

    function format_source_query($string)
    {
        $buffer = new ByteBuffer($string);
        $info['version']         = $buffer->ReadByte();
        $info['hostname']         = $buffer->ReadString();
        $info['map']             = $buffer->ReadString();
        $info['game']             = $buffer->ReadString();
        $info['gamedesc']         = $buffer->ReadString();
        $info['appid']             = $buffer->ReadShort();
        $info['players']         = $buffer->ReadByte();
        $info['maxplayers']     = $buffer->ReadByte();
        $info['bots']             = $buffer->ReadByte();
        $info['dedicated']         = $buffer->ReadChar(); // l - listen, d - dedicated, p - sourcetv
        $info['os']             = $buffer->ReadChar(); // l - linux, w - windows
        $info['password']         = $buffer->ReadByte(); // true/false
        $info['vac']             = $buffer->ReadByte(); // true/false
        $info['gameversion']     = $buffer->ReadString();

        return $info;
    }

    $query = source_query($address);
    $response = format_source_query($query);
Reply


Messages In This Thread
Query a server with PHP - [Toast] - 05-07-2007, 01:38 PM
RE: Query a server with PHP - love_admin - 07-03-2007, 05:05 AM
RE: Query a server with PHP - Drocona - 07-03-2007, 10:00 PM
RE: Query a server with PHP - maximm - 12-16-2007, 03:13 PM
RE: Query a server with PHP - nickkarma - 09-30-2007, 06:44 PM
RE: Query a server with PHP - nickkarma - 09-30-2007, 08:52 PM
RE: Query a server with PHP - Drefsab - 10-05-2007, 10:23 AM
RE: Query a server with PHP - Spartanfrog - 10-05-2007, 11:34 AM
RE: Query a server with PHP - Muppet - 10-05-2007, 07:11 PM
RE: Query a server with PHP - Spartanfrog - 12-17-2007, 12:05 AM
RE: Query a server with PHP - maximm - 12-17-2007, 06:25 AM
RE: Query a server with PHP - Spartanfrog - 12-18-2007, 07:33 AM
RE: Query a server with PHP - pob944 - 01-19-2008, 07:22 AM
RE: Query a server with PHP - Drocona - 01-19-2008, 09:22 AM
RE: Query a server with PHP - Spartanfrog - 01-19-2008, 12:30 PM
RE: Query a server with PHP - Jezternz - 04-20-2008, 04:47 PM
RE: Query a server with PHP - archwolf - 04-30-2008, 01:30 AM
RE: Query a server with PHP - noximana - 05-12-2008, 09:59 PM
RE: Query a server with PHP - Jezternz - 05-20-2008, 02:03 PM
RE: Query a server with PHP - [TgA] ATI - 10-27-2008, 03:22 AM
RE: Query a server with PHP - Herbi - 11-28-2008, 10:25 AM
RE: Query a server with PHP - trewq - 11-28-2008, 10:35 AM
RE: Query a server with PHP - Psychosis - 11-30-2008, 04:26 PM
RE: Query a server with PHP - atom241 - 07-03-2015, 06:39 AM
RE: Query a server with PHP - Herbi - 12-04-2008, 02:16 AM
RE: Query a server with PHP - _dp - 11-30-2009, 03:36 AM
RE: Query a server with PHP - realchamp - 11-30-2009, 06:34 AM
RE: Query a server with PHP - _dp - 11-30-2009, 07:24 AM
RE: Query a server with PHP - realchamp - 11-30-2009, 07:38 AM
RE: Query a server with PHP - _dp - 12-05-2009, 06:50 AM
RE: Query a server with PHP - d4f - 12-05-2009, 10:23 AM
RE: Query a server with PHP - _dp - 12-05-2009, 11:15 PM
RE: Query a server with PHP - Svpernaut - 12-27-2009, 07:14 PM
RE: Query a server with PHP - SaintGTR - 08-22-2010, 10:14 PM
RE: Query a server with PHP - realchamp - 08-22-2010, 11:34 PM
RE: Query a server with PHP - trewq - 08-23-2010, 06:33 AM
RE: Query a server with PHP - ilinx - 08-24-2010, 12:44 AM
RE: Query a server with PHP - Stinger - 08-24-2010, 11:21 AM
RE: Query a server with PHP - Chemicstry - 06-04-2012, 08:50 AM
RE: Query a server with PHP - LuxLOL - 09-13-2012, 08:06 AM
RE: Query a server with PHP - Racer - 12-28-2012, 07:56 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)