我正在尝试查明用户是否属于 Steam 组。为此,我使用 Steam 的 Web API,并使用 URL:
https://api.steampowered.com/ISteamUser/GetUserGroupList/v1/?key=APIKEYHERE&steamid=STEAMID64HERE
获取用户所属的所有组的 JSON 响应。 现在我想使用 PHP 查明他们是否属于我的 ID 为 1111111 的特定组。
人们会怎样做呢?目前我的代码如下所示:
$groupid = "1111111";
$url = "https://api.steampowered.com/ISteamUser/GetUserGroupList/v1/?key=APIKEYHERE&steamid=STEAMID64HERE";
$result = file_get_contents($url);
// Will dump a beauty json :)
$pretty = json_decode($result, true);
这使得 $pretty 变量包含整个 JSON 响应。 我将如何使用 PHP 在如下所示的响应中查找组 ID?
{
"response": {
"success": true,
"groups": [
{
"gid": "4458711"
},
{
"gid": "9538146"
},
{
"gid": "11683421"
},
{
"gid": "24781197"
},
{
"gid": "25160263"
},
{
"gid": "26301716"
},
{
"gid": "29202157"
},
{
"gid": "1111111"
}
]
}
}
想不通。 有什么帮助吗? :)
最佳答案
使用以下代码检查响应中是否存在用户
$groupid = "1111111";
$is_exists = false;
$url = "https://api.steampowered.com/ISteamUser/GetUserGroupList/v1/?key=APIKEYHERE&steamid=STEAMID64HERE";
$result = file_get_contents($url);
// Will dump a beauty json :)
$pretty = json_decode($result, true);
foreach ($pretty['response']['groups'] as $key => $value) {
if($value['gid'] == $groupid) {
$is_exists = true;
break;
}
}
// check is_exists
检查上述变量$is_exists
的真假
关于php - 使用 PHP 查找 JSON URL 响应中的特定条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46685588/