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. To do this, I'm using Steam's Web API, and using 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. Now I want to find out if they're part of my specific group with ID: 1111111 using PHP.

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. How would I use PHP to find the group ID in a response that looked like this?

    {
    "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? :)

2 answers

  • answered 2017-10-11 10:25 pravindot17

    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

  • answered 2017-10-11 10:29 Abhijit

            $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";  
            }   
    
        }