我一直在尝试使用 JSON 格式的 Steam Web API。我一直在尝试打印 API 给出的数组输出。
<?php
$id = $_GET['id'];
$key = 'xxx';
$link = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $key . '&steamids=' . $id . '&format=json');
$profile_info = json_decode($link);
$json_response = json_encode($profile_info->response);
print($json_response['steamid']);
?>
该密钥显然已被 Steam 生成器给我的密钥替换,但我的这段代码片段仅返回字符
{
,它应该返回 76561197989628470
,这是我的 steamid
。
这些是 JSON 格式的数组
{
"response": {
"players": [
{
"steamid": "76561197989628470",
"communityvisibilitystate": 3,
"profilestate": 1,
"personaname": "Archey",
"lastlogoff": 1334719151,
"commentpermission": 1,
"profileurl": "http://steamcommunity.com/id/Archey6/",
"avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/74/745b633a08937a5cf52bb44c2bdd3552f85455d7.jpg",
"avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/74/745b633a08937a5cf52bb44c2bdd3552f85455d7_medium.jpg",
"avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/74/745b633a08937a5cf52bb44c2bdd3552f85455d7_full.jpg",
"personastate": 1,
"primaryclanid": "103582791432066081",
"timecreated": 1177637717,
"loccountrycode": "CA",
"locstatecode": "SK"
}
]
}
为什么要先解码然后编码 json?
<?php
$id = $_GET['id'];
$key = 'xxx';
$link = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $key . '&steamids=' . $id . '&format=json');
$myarray = json_decode($link, true);
print $myarray['response']['players'][0]['steamid'];
?>
或者如果您确实需要再次编码:
<?php
$id = $_GET['id'];
$key = 'xxx';
$link = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $key . '&steamids=' . $id . '&format=json');
$profile_info = json_decode($link);
$json_response = json_encode($profile_info->response->players);
$decoded = json_decode($json_response, true);
print $json_response['steamid'];
?>