简体   繁体   English

使用PHP在JSON URL响应中查找特定条目

[英]Using PHP to find specific entry in JSON URL response

I am trying to find out whether a user is part of a Steam group. 我试图找出用户是否属于Steam组。 To do this, I'm using Steam's Web API, and using URL: 为此,我使用了Steam的Web API,并使用了URL:

https://api.steampowered.com/ISteamUser/GetUserGroupList/v1/?key=APIKEYHERE&steamid=STEAMID64HERE

To get a JSON response of all groups that a user is part of. 获取用户所属的所有组的JSON响应。 Now I want to find out if they're part of my specific group with ID: 1111111 using PHP. 现在,我想使用PHP查找它们是否属于我的ID为1111111的特定组。

How would one do that? 一个人怎么做? Currently I have the code being decoded like so: 目前,我的代码正在解码,如下所示:

$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);

This makes the $pretty variable contain the entire JSON response. 这使$ pretty变量包含整个JSON响应。 How would I use PHP to find the group ID in a response that looked like this? 我将如何使用PHP在看起来像这样的响应中找到组ID?

    {
    "response": {
        "success": true,
        "groups": [
            {
                "gid": "4458711"
            },
            {
                "gid": "9538146"
            },
            {
                "gid": "11683421"
            },
            {
                "gid": "24781197"
            },
            {
                "gid": "25160263"
            },
            {
                "gid": "26301716"
            },
            {
                "gid": "29202157"
            },
            {
                "gid": "1111111"
            }
        ]

    }
}

Can't figure it out. 无法解决。 Any help? 有什么帮助吗? :) :)

Use the below code to check whether user is exist in the response or not 使用以下代码检查响应中是否存在用户

$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

Check that above variable $is_exists for true or false 检查上面的变量$is_exists是否为true或false

        $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);
    $s = $pretty->response->groups;
    foreach($s as $value)
    {
        if((int) $groupid  ==  $value->gid)
        {
            echo  "Found";
        }else
        {
            echo  "Not found";  
        }   

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM