Search code examples
phpjsonsteam-web-api

Get a single item from JSON in PHP


I've seaching stack overflow and did not find what I`m trying to do ... So here is the question: HOW TO RETURN STEAMID ONLY?

CODE:

<?php
// define variables and set to empty values
$steamID = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $steamID = test_input($_POST["steamID"]);
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
$keyApp = '5DE00EE4D07BA728EDAB0B3C2961AA6B';
$api = "";
$json_data = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$keyApp&steamids=$steamID");
$array = json_decode($json_data, true);
var_dump($array);
?>

var_dump:

array(1) {
  ["response"]=>
  array(1) {
    ["players"]=>
    array(1) {
      [0]=>
      array(19) {
        ["steamid"]=>
        string(17) "76561198058543756"
        ["communityvisibilitystate"]=>
        int(3)
        ["profilestate"]=>
        int(1)
        ["personaname"]=>
        string(12) ""Cientistas""
        ["commentpermission"]=>
        int(1)
        ["profileurl"]=>
        string(37) "https://steamcommunity.com/id/rhatto/"
        ["avatar"]=>
        string(116) "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c9/c9442f1ea597a7685f8e50f2f7c922321beb4eba.jpg"
        ["avatarmedium"]=>
        string(123) "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c9/c9442f1ea597a7685f8e50f2f7c922321beb4eba_medium.jpg"
        ["avatarfull"]=>
        string(121) "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c9/c9442f1ea597a7685f8e50f2f7c922321beb4eba_full.jpg"
        ["avatarhash"]=>
        string(40) "c9442f1ea597a7685f8e50f2f7c922321beb4eba"
        ["lastlogoff"]=>
        int(1593317334)
        ["personastate"]=>
        int(0)
        ["realname"]=>
        string(44) "por erro em estudo sobre a hidroxicloroquina"
        ["primaryclanid"]=>
        string(18) "103582791454640235"
        ["timecreated"]=>
        int(1328817888)
        ["personastateflags"]=>
        int(0)
        ["loccountrycode"]=>
        string(2) "CN"
        ["locstatecode"]=>
        string(2) "32"
        ["loccityid"]=>
        int(9982)
      }
    }
  }
}

How can i extract steamid from $array? Already tryed this links https://www.codewall.co.uk/how-to-read-json-file-using-php-examples/ https://www.php.net/manual/pt_BR/function.json-decode.php


Solution

  • By accessing the correct path to the steamid the only thing is, apparently you might have multiple items in the players array. So you'll probably need a loop of some sorts.

    Something like this maybe:

    $players = $array['response']['players'];
    
    foreach($players as $player) {
      echo $player['steamid'] . '<br>';
    }
    

    Or if you always have 1 player in your players array you could also do this (quick and dirty):

    echo $array['response']['players'][0]['steamid'];