TOLEk
TOLEk

Reputation: 21

Sorting Steam Web Api GetPlayerSummaries response

I'm making class which will get data about teams - 5 Steam users basing on 32bit SteamIDs stored in database for each team. It's translated then to 64bit SteamID. I need response in correct order, because there is specified captain of the team. And here's the problem - GetPlayerSummaries always returns profiles in random order. I want these to be sorted like in url. I've tried already sort() methods, but it seems not working, like I want to. I've tried matching 'steamid' with this translated 64 bit SteamID like this:

$profile_get = json_decode(file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=mywebapikey&steamids='.$stmid64['capt'].','.$stmid64['p2'].','.$stmid64['p3'].','.$stmid64['p4'].','.$stmid64['p5']),true);

$profile_get = $profile_get['response'];

    foreach($profile_get['players'] as $profile){
        if($profile['steamid'] === $stmid64['capt']){
            $profile_got = array(
               0 => $profile
            );
        }
        elseif($profile['steamid'] === $stmid64['p2']){
            $profile_got[1] = $profile;
        }
        elseif($profile['steamid'] === $stmid64['p3']){
            $profile_got[2] = $profile;
        }
        elseif($profile['steamid'] === $stmid64['p4']){
            $profile_got[3] = $profile;
        }
        elseif($profile['steamid'] === $stmid64['p5']){
            $profile_got[4] = $profile;
        }
    }

where $stmid64 is 64bit SteamID, but it obviously don't work :(

var_dump($profile_got[0]);
var_dump($profile_got[1]);
var_dump($profile_got[2]);
var_dump($profile_got[3]);
var_dump($profile_got[4]);

and var_dump($profile_got); returns NULL. I've tried many different codes, but they didn't work also.

I hope you can help me with not doing all requests separately.

Upvotes: 1

Views: 875

Answers (1)

$profile_get = json_decode(file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.$mywebapikey.'&steamids='.$stmid64['capt'].','.$stmid64['p2'].','.$stmid64['p3'].','.$stmid64['p4'].','.$stmid64['p5']),true);

for ($i = 0; $i < 5; $i++) {
  if ($i == 0)
    $player = 'capt';
  else 
    $player = 'p'.($i+1);

  for ($j = 0; $j < 5; $j++) {
    if ($stmid64[$player] == $profile_get['response']['players'][$j]['steamid']) {
      $profile_got[$i] = $profile_get['response']['players'][$j];
      break;
    }
  } 
} 

var_dump($profile_got[0]);
var_dump($profile_got[1]);
var_dump($profile_got[2]);
var_dump($profile_got[3]);
var_dump($profile_got[4]);

cheers

this will work as intended, it will order your array by 'capt' (why not p1 instead ? , you could save 3 lines), 'p2', 'p3', 'p4', 'p5'. you can still extend this in many ways but basically this is how to put them in the right order. also mind that i stored your (well mine) api key inside a $var

Upvotes: 0

Related Questions