View Single Post
4ever16
Veteran Member
Join Date: Apr 2015
Old 02-19-2020 , 08:28   Re: Get players and steam id with php rcon
Reply With Quote #18

For those who wants to use this code this one is the correct one. Fysiskt forgott to remove 1 empty line / array.

You will also need this file "SourceQuery.class.php" in the same map.
You can download the file here: https://github.com/xPaw/PHP-Source-Q...er/SourceQuery

PHP Code:
<?php
require 'SourceQuery.class.php';    
$Query = new SourceQuery( );
    
try
{
$Query->Connect'SERVER IP'27015 );        
$Query->SetRconPassword'PASSWORD' );            
$myStatus $Query->Rcon'status' );    

function 
parse_hlds_status($StatusString)
{
    
// parse everything up to the player list
    
$StatusArray preg_split("/(\r\n|\n|\r)/"$StatusString);
    
    
$out['hostname'] = array_shift($StatusArray);
    
$out['version'] = array_shift($StatusArray);
    
$out['tcp/ip'] = array_shift($StatusArray);
    
$out['map'] = array_shift($StatusArray);
    
$out['maxplayers'] = array_shift($StatusArray);
    
    
array_shift($StatusArray); // Get rid of blank line
    
    // Parse player list
    
$PlayerListString $StatusArray// Including header row
    
array_pop($PlayerListString);
    unset(
$PlayerListString[count($PlayerListString) - 1]);
    
    
// Parse headers
    
$header explode_by_whitespace(array_shift($PlayerListString));
    
    
// Parse players
    
$PlayerList array_map('parse_player_line'$PlayerListString);
    
array_walk($PlayerList'_combine_array'$header);
    
    
$out['Players'] = $PlayerList;
    
    return 
$out;
}

// Define private functions
function _combine_array(&$row$key$header)
{
    
$row array_combine(array_intersect_key($header$row), array_intersect_key($row$header));


function 
parse_player_line($string)
{
    
$temp = array();
    
$temp[0] = trim(substr($string12)); // Get the fixed-width player slot number
    
$temp array_merge($tempexplode_by_whitespace(substr($string4))); // The reset is whitespace delimited (not space delimited)
    
return $temp;
}

function 
explode_by_whitespace($string)
{
    
$pattern '/[^\s"\']+|"([^"]*)"|\'([^\']*)\'/';
    
$matches = array();
    
$i 0;
    
$out = array();
    while( 
preg_match($pattern$string$matchesPREG_OFFSET_CAPTURE$i) )
    {
        
array_push($out$matches[0][0]);
        
$i $matches[0][1] + strlen($matches[0][0]);
    }
    return 
$out;
}

$status parse_hlds_status($myStatus);
foreach( 
$status['Players'] as $player)
{
    print(
$player['name'] . "\t" $player['uniqueid'] . "\n");


$Query->Disconnect( );
    }
catch( 
SQueryException $e )
    {
$Query->Disconnect( );
    }    
?>

Last edited by 4ever16; 02-19-2020 at 08:29.
4ever16 is offline