Raised This Month: $12 Target: $400
 3% 

Solved Get players and steam id with php rcon


Post New Thread Reply   
 
Thread Tools Display Modes
4ever16
Veteran Member
Join Date: Apr 2015
Old 02-15-2020 , 06:11   Re: Get players and steam id with php rcon
Reply With Quote #11

Ok so i use this code now.
PHP Code:
<?php
require 'SourceQuery.class.php';    
$Query = new SourceQuery( );
    
try
{
$Query->Connect'SERVER IP'27015 );        
$Query->SetRconPassword'RCON PASSWORD' );     
       
$YourStatusString $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);
    
    
// Parse headers
    
$header str_getcsv(array_shift($PlayerListString), $delimiter " ");
    
    
// 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($header$row);
}

function 
parse_player_line($string)
{
    
$temp str_getcsv($string$delimiter " ");
    
array_shift($temp);
    return 
$temp;




print_r(parse_hlds_status($YourStatusString)); 
    


$Query->Disconnect( );
    }
catch( 
SQueryException $e )
    {
$Query->Disconnect( );
    }
echo 
'';
?>
Getting this error.
PHP Code:
Warningarray_combine(): Both parameters should have an equal number of elements in mywebsite.com/public_html/rcon/ServerRcon.php on line 43 
Line 43 is this code.
PHP Code:
    $row array_combine($header$row); 
4ever16 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-16-2020 , 04:51   Re: Get players and steam id with php rcon
Reply With Quote #12

When I use the status output that you posted in the OP, it works just fine. So, at this point, and since this is not a PHP forum, you're going to need to debug it yourself.
__________________
fysiks is offline
4ever16
Veteran Member
Join Date: Apr 2015
Old 02-16-2020 , 07:16   Re: Get players and steam id with php rcon
Reply With Quote #13

Quote:
Originally Posted by fysiks View Post
You are not passing your status string to the function. You need to replace "$YourStatusString" with your variable.
Well one question did you test the code before posting?

Ok so look i did what you sed now and it prints this.
PHP Code:
Warningarray_combine(): Both parameters should have an equal number of elements in mywebsite.com/public_html/rcon/rcon.php on line 43 
Line 43 is this line.
PHP Code:
 $row array_combine($header$row); 
PHP Code:
<?php
require 'SourceQuery.class.php';    
$Query = new SourceQuery( );
    
try
{
$Query->Connect'SERVER IP'27015 );        
$Query->SetRconPassword'PASSWORD' );            
$YourStatusString $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);
    
    
// Parse headers
    
$header str_getcsv(array_shift($PlayerListString), $delimiter " ");
    
    
// 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($header$row);
}

function 
parse_player_line($string)
{
    
$temp str_getcsv($string$delimiter " ");
    
array_shift($temp);
    return 
$temp;




print_r(parse_hlds_status($YourStatusString)); 
    


$Query->Disconnect( );
    }
catch( 
SQueryException $e )
    {
$Query->Disconnect( );
    }
?>
Then i googled the error and found this thread: https://stackoverflow.com/questions/...ferent-lengths

So i used this code on line 43.
PHP Code:
// Define private functions
function _combine_array(&$row$key$header)
{
    
$row array_combine(array_intersect_key($header$row), array_intersect_key($row$header));

With this new code it prints wierd stuff. Steam id and user name is showing in some arrays and some not. So basiccly there is something wrong with your code. Do you have a solution and can u test stuff before posting?

This is what it prints with that new code @ line 43. When it printed this there were 14 users online but as i sed steam id and nickname is not printed correctly. Some are missing and so on. The error i posted is the problem anyways.

PHP Code:
array(6) {
  [
"hostname"]=>
  
string(61"lhostname:  SERVER NAME"
  
["version"]=>
  
string(45"version :  48/1.1.2.7/Stdio 2117 secure  (10)"
  
["tcp/ip"]=>
  
string(30"tcp/ip  :  SERVER IP"
  
["map"]=>
  
string(37"map     :  de_dust2 at: 0 x, 0 y, 0 z"
  
["maxplayers"]=>
  
string(29"players :  14 active (15 max)"
  
["Players"]=>
  array(
15) {
    [
0]=>
    array(
10) {
      [
"#"]=>
      
string(1"1"
      
[""]=>
      
string(0""
      
["name"]=>
      
string(1"G"
      
["userid"]=>
      
string(3"779"
      
["uniqueid"]=>
      
string(18"STEAM_0:1:32244765"
      
["frag"]=>
      
string(0""
      
["time"]=>
      
string(0""
      
["ping"]=>
      
string(1"0"
      
["loss"]=>
      
string(5"05:09"
      
["adr"]=>
      
string(0""
    
}
    [
1]=>
    array(
10) {
      [
"#"]=>
      
string(1"2"
      
[""]=>
      
string(3"763"
      
["name"]=>
      
string(18"STEAM_0:0:16630816"
      
["userid"]=>
      
string(0""
      
["uniqueid"]=>
      
string(2"13"
      
["frag"]=>
      
string(5"25:00"
      
["time"]=>
      
string(0""
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(1"4"
    
}
    [
2]=>
    array(
10) {
      [
"#"]=>
      
string(1"3"
      
[""]=>
      
string(0""
      
["name"]=>
      
string(1"0"
      
["userid"]=>
      
string(5"25:20"
      
["uniqueid"]=>
      
string(0""
      
["frag"]=>
      
string(0""
      
["time"]=>
      
string(2"23"
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(0""
    
}
    [
3]=>
    array(
10) {
      [
"#"]=>
      
string(1"4"
      
[""]=>
      
string(2"17"
      
["name"]=>
      
string(5"45:19"
      
["userid"]=>
      
string(0""
      
["uniqueid"]=>
      
string(0""
      
["frag"]=>
      
string(2"34"
      
["time"]=>
      
string(0""
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(1"0"
    
}
    [
4]=>
    array(
10) {
      [
"#"]=>
      
string(1"5"
      
[""]=>
      
string(0""
      
["name"]=>
      
string(1"8"
      
["userid"]=>
      
string(5"22:41"
      
["uniqueid"]=>
      
string(0""
      
["frag"]=>
      
string(0""
      
["time"]=>
      
string(2"13"
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(0""
    
}
    [
5]=>
    array(
10) {
      [
"#"]=>
      
string(1"6"
      
[""]=>
      
string(2"15"
      
["name"]=>
      
string(0""
      
["userid"]=>
      
string(7"1:09:45"
      
["uniqueid"]=>
      
string(0""
      
["frag"]=>
      
string(0""
      
["time"]=>
      
string(0""
      
["ping"]=>
      
string(1"8"
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(0""
    
}
    [
6]=>
    array(
10) {
      [
"#"]=>
      
string(1"7"
      
[""]=>
      
string(2"hr"
      
["name"]=>
      
string(3"766"
      
["userid"]=>
      
string(19"STEAM_0:0:449414351"
      
["uniqueid"]=>
      
string(0""
      
["frag"]=>
      
string(0""
      
["time"]=>
      
string(1"9"
      
["ping"]=>
      
string(5"22:14"
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(0""
    
}
    [
7]=>
    array(
10) {
      [
"#"]=>
      
string(1"8"
      
[""]=>
      
string(18"STEAM_0:0:26858302"
      
["name"]=>
      
string(0""
      
["userid"]=>
      
string(0""
      
["uniqueid"]=>
      
string(1"2"
      
["frag"]=>
      
string(5"21:58"
      
["time"]=>
      
string(0""
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(2"17"
      
["adr"]=>
      
string(0""
    
}
    [
8]=>
    array(
10) {
      [
"#"]=>
      
string(1"9"
      
[""]=>
      
string(0""
      
["name"]=>
      
string(1"3"
      
["userid"]=>
      
string(5"21:42"
      
["uniqueid"]=>
      
string(0""
      
["frag"]=>
      
string(0""
      
["time"]=>
      
string(2"26"
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(0""
    
}
    [
9]=>
    array(
10) {
      [
"#"]=>
      
string(16"101-587-120-9874"
      
[""]=>
      
string(1"0"
      
["name"]=>
      
string(5"25:16"
      
["userid"]=>
      
string(0""
      
["uniqueid"]=>
      
string(0""
      
["frag"]=>
      
string(2"20"
      
["time"]=>
      
string(0""
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(1"0"
    
}
    [
10]=>
    array(
10) {
      [
"#"]=>
      
string(31"kilowatt1337           ï¼ƒZowie"
      
[""]=>
      
string(5"18:57"
      
["name"]=>
      
string(0""
      
["userid"]=>
      
string(0""
      
["uniqueid"]=>
      
string(2"11"
      
["frag"]=>
      
string(0""
      
["time"]=>
      
string(0""
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(1"0"
      
["adr"]=>
      
string(19"ip"
    
}
    [
11]=>
    array(
10) {
      [
"#"]=>
      
string(29"Mustafa al Baghdadi! Malickis"
      
[""]=>
      
string(1"6"
      
["name"]=>
      
string(5"08:01"
      
["userid"]=>
      
string(0""
      
["uniqueid"]=>
      
string(0""
      
["frag"]=>
      
string(0""
      
["time"]=>
      
string(1"9"
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(0""
    
}
    [
12]=>
    array(
10) {
      [
"#"]=>
      
string(0""
      
[""]=>
      
string(18"STEAM_0:0:45222538"
      
["name"]=>
      
string(0""
      
["userid"]=>
      
string(0""
      
["uniqueid"]=>
      
string(1"2"
      
["frag"]=>
      
string(5"10:59"
      
["time"]=>
      
string(0""
      
["ping"]=>
      
string(0""
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(1"9"
    
}
    [
13]=>
    array(
10) {
      [
"#"]=>
      
string(0""
      
[""]=>
      
string(0""
      
["name"]=>
      
string(0""
      
["userid"]=>
      
string(1"0"
      
["uniqueid"]=>
      
string(5"03:05"
      
["frag"]=>
      
string(0""
      
["time"]=>
      
string(0""
      
["ping"]=>
      
string(2"60"
      
["loss"]=>
      
string(0""
      
["adr"]=>
      
string(0""
    
}
    [
14]=>
    array(
1) {
      [
"#"]=>
      
string(5"users"
    
}
  }


Last edited by 4ever16; 02-16-2020 at 07:27.
4ever16 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-16-2020 , 15:31   Re: Get players and steam id with php rcon
Reply With Quote #14

Quote:
Originally Posted by 4ever16 View Post
Well one question did you test the code before posting?

Uh . . that's how I wrote the code, by testing it. Also I already said as much:

Quote:
Originally Posted by fysiks View Post
When I use the status output that you posted in the OP, it works just fine.
So, if your string has any more or less output (the only exception being the number of players) than what you posted, it won't work properly.
__________________

Last edited by fysiks; 02-16-2020 at 15:32.
fysiks is offline
4ever16
Veteran Member
Join Date: Apr 2015
Old 02-16-2020 , 16:14   Re: Get players and steam id with php rcon
Reply With Quote #15

Quote:
Originally Posted by fysiks View Post
Uh . . that's how I wrote the code, by testing it. Also I already said as much:

So, if your string has any more or less output (the only exception being the number of players) than what you posted, it won't work properly.
Don't understand what you are saying do u know what the error can be and how to fix it? Its legal steam server. You also see what cind of output i get. It looks wierd. Steam ids are printied in some arrays and not in some.

Last edited by 4ever16; 02-16-2020 at 16:14.
4ever16 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-16-2020 , 18:03   Re: Get players and steam id with php rcon
Reply With Quote #16

Ok, so based on the unedited status output that you gave me in PM, I found that your server was adding random spaces into the player list section of the status output.

I've updated my function here to account for this extra whitespace.

Now you can use the result to loop through all the players and get the data you need:

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

__________________

Last edited by fysiks; 02-16-2020 at 18:08.
fysiks is offline
4ever16
Veteran Member
Join Date: Apr 2015
Old 02-16-2020 , 18:45   Re: Get players and steam id with php rcon
Reply With Quote #17

Yes now it works as it should! Thanks!

Last edited by 4ever16; 02-16-2020 at 18:46.
4ever16 is offline
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
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-19-2020 , 23:51   Re: Get players and steam id with php rcon
Reply With Quote #19

Quote:
Originally Posted by 4ever16 View Post
For those who wants to use this code this one is the correct one. Fysiskt forgott to remove 1 empty line / array.
I did not "forget" to do anything. The status output that I tested the code with does not require this. However, since you refuse to send me your status output (that is not copy/pasted to preserve potential whitespace), I can't properly adjust the code to work in both situations. It is most likely something simple that needs to be handled differently than you've implemented.
__________________

Last edited by fysiks; 02-20-2020 at 00:05.
fysiks is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-24-2020 , 00:04   Re: Get players and steam id with php rcon
Reply With Quote #20

I've updated the function to fully process the server data and updated the robustness of the parsing and shouldn't have issues with anomalous white space differences (which is probably what 4ever16 was dealing with).

The code is attached here.
__________________
fysiks is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:20.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode