PHP json_decode doesn`t work
Asked Answered
E

2

5

Hi i want to get the personaname of steam user I have storaged data in files in .json format.

{
"response": {
    "players": [
        {
            "steamid": "76561198137714668",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "UareBugged",
            "lastlogoff": 1418911040,
            "commentpermission": 1,
            "profileurl": "http://steamcommunity.com/id/uarenotbest/",
            "avatar": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21.jpg",
            "avatarmedium": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21_medium.jpg",
            "avatarfull": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21_full.jpg",
            "personastate": 1,
            "realname": "Michal Šlesár",
            "primaryclanid": "103582791436765601",
            "timecreated": 1400861961,
            "personastateflags": 0,
            "loccountrycode": "SK",
            "locstatecode": "08"
        }
    ]

}

}

And i want to get the personaname to variable but it doing nothing , variable is empty i think json_decode doesn`t work but i really dont know.

    $pname = json_decode(file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v002/?key=KEYCONSORED&Steamids={$_SESSION['T2SteamID64']}"));
    echo $pname['response']['players']['personaname'];

echo is empty

Emanate answered 18/12, 2014 at 16:18 Comment(3)
What does var_dump($pname); show?Marta
print_r($pname); does anthing?Regent
print_r(json_decode($pname, true)); and $pname['response']['players'][0]['personaname'];Madonna
C
9

Players is an array:

$pname['response']['players'][0]['personaname'];
Caponize answered 18/12, 2014 at 16:20 Comment(0)
T
4

Several errors here.

Let me explain one by one giving tips to find common errors on PHP JSON decode/encode.

1. Invalid JSON

First, your JSON is invalid, it's missing a ending } at the end.

Update: just after @tftd comment I saw you formatted your code wrongly, but anyway, let me explain how to find issues, because this is not trivial as it should be in PHP. The other errors still are valid ones.

To check why json_decode is not working, use json_last_error: it'll return an error number, which means:

0 = JSON_ERROR_NONE = "No error has occurred"
1 = JSON_ERROR_DEPTH = "The maximum stack depth has been exceeded"
2 = JSON_ERROR_STATE_MISMATCH  = "Invalid or malformed JSON"
3 = JSON_ERROR_CTRL_CHAR = "Control character error, possibly incorrectly encoded"
4 = JSON_ERROR_SYNTAX = "Syntax error"
5 = JSON_ERROR_UTF8 = "Malformed UTF-8 characters, possibly incorrectly encode"
6 = JSON_ERROR_RECURSION = "One or more recursive references in the value to be encoded"
7 = JSON_ERROR_INF_OR_NAN = "One or more NAN or INF values in the value to be encoded"
8 = JSON_ERROR_UNSUPPORTED_TYPE = "A value of a type that cannot be encoded was given"

In your case, it was returning 4. So, I gone validate your JSON at http://jsonlint.com and I found the missing } at the end.

2. json_decode returns objects, not arrays

If you want to access one $pname as array, you need to chance your json_decode line to:

$pname = json_decode(file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v002/?key=KEYCONSORED&Steamids={$_SESSION['T2SteamID64']}"), true);

Note the last parameter, true for the json_decode method. According the documentation, when true, returned objects will be converted into associative arrays.

3. players is an array

Fixed your JSON and json_decode call, we can see players is an array. So, if you want read the first player, use:

$pname['response']['players'][0]

Fixed Code

I'm not reading from a URL, so I used a heredoc:

<?php

$content = <<<EOD
{
"response": {
    "players": [
        {
            "steamid": "76561198137714668",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "UareBugged",
            "lastlogoff": 1418911040,
            "commentpermission": 1,
            "profileurl": "http://steamcommunity.com/id/uarenotbest/",
            "avatar": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21.jpg",
            "avatarmedium": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21_medium.jpg",
            "avatarfull": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21_full.jpg",
            "personastate": 1,
            "realname": "Michal Šlesár",
            "primaryclanid": "103582791436765601",
            "timecreated": 1400861961,
            "personastateflags": 0,
            "loccountrycode": "SK",
            "locstatecode": "08"
        }
    ]

 }
}
EOD;

$pname = json_decode($content, true);
echo $pname['response']['players'][0]['personaname'];

This will output, as expected, UareBugged.

Trocki answered 18/12, 2014 at 16:43 Comment(3)
Just one note - the json is valid, the user just didn't format it correctly. The missing } is outside the formatting.Caponize
@tftd, fair enough (I gave you a +1). But the other errors are still valid one. And I'll keep the invalid json talk, because find that kind of error is not obvious and it should be.Trocki
Yes, that's usually easy to overlook. :)Caponize

© 2022 - 2024 — McMap. All rights reserved.