Teknikk
Teknikk

Reputation: 251

json Problems to echo, arrays

I am working on a script, that will use steam api, and i selected to use json for the response format. So i have used var_dump with and without jason_decode() and it appears to be ok. But can't manage to print it out, or echo it.

Script that gets the json data

<?php
$id = $_GET['SteamId'];
$get = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=API_KEY_REMOVED_FOR_SECURITY&steamids=$id",true);
$data = json_decode($get);
//var_dump($data);
echo $data->realname;
?>

So, that i get using the var_dump with json_decode is this.

object(stdClass)#1 (1) { ["response"]=> object(stdClass)#2 (1) { ["players"]=> array(1) { [0]=> object(stdClass)#3 (15) { ["steamid"]=> string(17) "76561198053511970" ["communityvisibilitystate"]=> int(3) ["profilestate"]=> int(1) ["personaname"]=> string(9) "Undefined" ["lastlogoff"]=> int(1340978067) ["profileurl"]=> string(41) "http://steamcommunity.com/id/Heisteknikk/" ["avatar"]=> string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/5c/5c75278da69102d9c8290bccd1becbb4081954cd.jpg" ["avatarmedium"]=> string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/5c/5c75278da69102d9c8290bccd1becbb4081954cd_medium.jpg" ["avatarfull"]=> string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/5c/5c75278da69102d9c8290bccd1becbb4081954cd_full.jpg" ["personastate"]=> int(1) ["realname"]=> string(7) "Andreas" ["primaryclanid"]=> string(18) "103582791430704052" ["timecreated"]=> int(1322427688) ["loccountrycode"]=> string(2) "NO" ["locstatecode"]=> string(2) "09" } } } }

And the raw data from the json.

{
    "response": {
        "players": [
            {
                "steamid": "76561198053511970",
                "communityvisibilitystate": 3,
                "profilestate": 1,
                "personaname": "Undefined",
                "lastlogoff": 1340978067,
                "profileurl": "http:\/\/steamcommunity.com\/id\/Heisteknikk\/",
                "avatar": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/5c\/5c75278da69102d9c8290bccd1becbb4081954cd.jpg",
                "avatarmedium": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/5c\/5c75278da69102d9c8290bccd1becbb4081954cd_medium.jpg",
                "avatarfull": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/5c\/5c75278da69102d9c8290bccd1becbb4081954cd_full.jpg",
                "personastate": 1,
                "realname": "Andreas",
                "primaryclanid": "103582791430704052",
                "timecreated": 1322427688,
                "loccountrycode": "NO",
                "locstatecode": "09"
            }
        ]

    }
}

I've been searching around on google about printing the json using "echo $data->realname;". So i don't know what i did wrong, so it canno't echo the data.

Upvotes: 0

Views: 210

Answers (1)

sam
sam

Reputation: 2984

Intro

Hey there, this is a REALLY old question, but since there's no answer here.

You do have it right when it comes to decoding the JSON PHP has the built-in function json_decode which can easily decode the function for you:

$json = '{...}';
$obj = json_decode($json, [ASSOC_ARRAY = FALSE]);

The second Param you can set to bool for an associative array which would change mentions from:

$obj->response;

To

$obj['response'];

Response

Now you say you need the realname from the players, that would be as simple as getting it via the player's position in the array, or just looping through the array:

echo $obj->response->players[0]->realname; // Andreas

Or if you have multiple users:

for( $i = 0; $i < count($obj->response->players); $i++ ) {
  echo $obj->response->players[$i]->realname;
}

Which would display all of the realname(s)

Upvotes: 1

Related Questions