5

嗨,我想获取 Steam 用户的人名,我已将数据存储在 .json 格式的文件中。

{
"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"
        }
    ]

}

}

而且我想将人物命名为变量,但它什么也没做,变量为空我认为 json_decode 不起作用但我真的不知道。

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

回声是空的

4

2 回答 2

9

玩家是一个数组:

$pname['response']['players'][0]['personaname'];
于 2014-12-18T16:20:54.960 回答
4

这里有几个错误。

让我一一解释在 PHP JSON 解码/编码中查找常见错误的提示。

1.无效的JSON

首先,您的 JSON 无效,最后缺少}结尾。

更新:就在@tftd 评论之后,我看到你错误地格式化了你的代码,但无论如何,让我解释一下如何找到问题,因为这在 PHP 中应该不是微不足道的。其他错误仍然是有效的。

要检查 json_decode 为什么不起作用,请使用json_last_error: 它将返回一个错误号,这意味着:

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"

在你的情况下,它正在返回4因此,我在http://jsonlint.com上验证了您的 JSON,最后发现了丢失}的内容。

2. json_decode 返回对象,而不是数组

如果您想将一个$pname作为数组访问,则需要将您的json_decode行改为:

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

注意方法的最后一个true参数json_decode。根据文档,when true,返回的对象将被转换为关联数组。

3. player 是一个数组

修正了你的 JSON 和json_decode调用,我们可以看到players是一个数组。因此,如果您想阅读第一个播放器,请使用:

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

固定代码

我不是从 URL 读取的,所以我使用了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'];

正如预期的那样,这将输出UareBugged.

于 2014-12-18T16:43:18.700 回答