How to get the Max Viewers of an Game out by Streamers, and specify wich one?

 $gameName = htmlspecialchars($_POST['name']);
 $channelsApi = '';
 $clientId = 'xxx';
 $ch = curl_init();
 $apiVers = '&api_version=5';
 curl_setopt_array($ch, array(
       'Client-ID: ' . $clientId
    CURLOPT_URL => $channelsApi . $gameName . $apiVers

 $response = curl_exec($ch);


$json_arrayX = var_dump(json_decode($response, true));

echo "</br>";echo "</br>";echo "</br>";

echo $json_array['data'][0]['viewer_count'];


Thats the code I have yet, it sorts by ViewerCount, but how to get out the max und its username?


This endpoint doesn’t support the parameters you have specified

This is no sort control. Generally the streams are sorted by view count anyway

To perform a lookup by game, you’ll need the game_id for the game as documented

game_id string Returns streams broadcasting a specified game ID. You can specify up to 100 IDs.

The Games API is documented here


Assuming you want german from your original URL


Thanks, so how to read it out the user_name into an php variable, would be very nice if thats answered, too! :slight_smile:


Is it possible to resolve game name into game id?


Yes i already answered that here

Parse the JSON response according to your needs.

Helix returns a array of one array containing the information you need, as documented.

$resp = json_decode($response);
$name = $resp->data[0]->user_name;


so i am new in JSON, have you maybe an sample code for me?


Should work for PHP. This is basic PHP array/object manipulation/reading, after using json_decode


Great thanks! :slight_smile:


Should that not echo game id?

$gameName = htmlspecialchars($_POST['name1']);
 $getGameID = '';
 $clientId = 'xxx';
 $ch = curl_init();
 $apiVers = '&api_version=5';
 curl_setopt_array($ch2, array(
       'Client-ID: ' . $clientId
    CURLOPT_URL => $getGameID . $gameName . $apiVers

 $response2 = curl_exec($ch2);

$resp = json_decode($response2);
$gameID = $resp->data[0]->game_id;


I get this error:

array(3) { ["error"]=&gt; string(11) "Bad Request" ["status"]=&gt; int(400) ["message"]=&gt; string(41) "Cannot pass empty string as lookup value." }


Is not required with helix.

Your URL is constructed wrong, as documented

it’s just name not game_name

This should work, and is a little cleaner and tests for errors than yours.


    $ch = curl_init('')
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Client-ID: ' . $clientId
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);

    if ($info['http_code'] == 200) {
        $resp = json_decode($result);
        if (json_last_error() == JSON_ERROR_NONE) {
            if ($resp->data && count($resp->data) == 1) {
                $gameID = $resp->data[0]->id;
                // go and do other stuff
            } else {
                echo 'Not one game returned';
        } else {
            echo 'JSON Parse Error';
    } else {
        echo 'Failed';

closed #13

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.