0

我正在尝试使用 Steam 登录创建一个网站,但是当我尝试从 JSON 调用一个值时,它不起作用。除了获取 JSON 值外,一切都在源代码中有效。我什至尝试打印蒸汽 ID,所以我知道 ID 有效。该 URL 也有效。

这是我的源代码

<?php
require 'openid.php';
try {
    $openid = new LightOpenID('workinganonymouswebsite.com');
    if (!$openid->mode) {
        $openid->identity = 'http://steamcommunity.com/openid';
        header('Location: ' . $openid->authUrl());
    } elseif ($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
        $steamurl = ($openid->validate() ? $openid->identity . '' : 'error');
        if ($steamurl == 'error') {
            print "There was an error signing in.";
        } else {
            $id          = end(explode('/', $steamurl));
            $jsonurl     = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXX&steamids=" . $id . "&format=json";
            $json        = file_get_contents($jsonurl, 0, null, null);
            $json_output = json_decode($json);
            echo $json_output['players']['personaname'];
        }
    }
} catch (ErrorException $e) {
    echo $e->getMessage();
}
?>

这是网站上的 JSON。

{
"response": {
    "players": [
        {
            "steamid": "76561198049205920",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "baseman101",
            "lastlogoff": 1357603378,
            "profileurl": "http://steamcommunity.com/id/baseman101/",
            "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/24/24bb7c0505db7efe1f1a602d09a5ea412e0ab4bd.jpg",
            "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/24/24bb7c0505db7efe1f1a602d09a5ea412e0ab4bd_medium.jpg",
            "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/24/24bb7c0505db7efe1f1a602d09a5ea412e0ab4bd_full.jpg",
            "personastate": 1,
            "primaryclanid": "103582791429521408",
            "timecreated": 1316469294,
            "loccountrycode": "US",
            "locstatecode": "VA",
            "loccityid": 3918
        }
    ]

}
}

我试过用谷歌搜索一切。如果有什么我错过了,我很抱歉。

4

1 回答 1

1

感谢您所有的帮助。我基本上把 JSON 代码放在一个变量中,从 Steam 网站上检索它。这是最好的解决方案,我会坚持下去。

<?php
require 'openid.php';
try {
$openid = new LightOpenID('blah.com');
if (!$openid->mode) {
    $openid->identity = 'http://steamcommunity.com/openid';
    header('Location: ' . $openid->authUrl());
} elseif ($openid->mode == 'cancel') {
    echo 'User has canceled authentication!';
} else {
    $steamurl = ($openid->validate() ? $openid->identity . '' : 'error');
    if ($steamurl == 'error') {
        print "There was an error signing in.";
    } else {
        $id          = end(explode('/', $steamurl));
        $context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
        $json_source = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXX&steamids=" . $id . "&format=json",false,$context);
        $json_output = json_decode($json_source,true);
        $json_output->response->players[0]->personaname;
        echo $json_output["response"]["players"][0]["personaname"];
    }
}
} catch (ErrorException $e) {
echo $e->getMessage();
}
?>

谢谢你,Passerby 和 hakre 的帮助。

将来,我必须创建 cookie 和所有简单的东西。实际上我现在就开始了。

于 2013-01-09T01:27:22.370 回答