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
Players is an array:
$pname['response']['players'][0]['personaname'];
Several errors here.
Let me explain one by one giving tips to find common errors on PHP JSON decode/encode.
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.
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.
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]
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With