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


#1
<?php
 $gameName = htmlspecialchars($_POST['name']);
 $channelsApi = 'https://api.twitch.tv/helix/streams?sort=viewer_count&language=de&offset=900&limit=100&game=';
 $clientId = 'xxx';
 $ch = curl_init();
 $apiVers = '&api_version=5';
 
 curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi . $gameName . $apiVers
 ));

 $response = curl_exec($ch);

echo"</br>";

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

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

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

#2

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


#3

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

So

https://api.twitch.tv/helix/streams?first=1&game_id=123&language=de

Assuming you want german from your original URL


#4

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


#5

Is it possible to resolve game name into game id?


#6

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;

#7

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


#8

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


#9

Great thanks! :slight_smile:


#10

Should that not echo game id?

$gameName = htmlspecialchars($_POST['name1']);
 $getGameID = 'https://api.twitch.tv/helix/games?game_name=';
 $clientId = 'xxx';
 $ch = curl_init();
 $apiVers = '&api_version=5';
 
 curl_setopt_array($ch2, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $getGameID . $gameName . $apiVers
 ));

 $response2 = curl_exec($ch2);

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

#11

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." }


#12

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.

<?php

    $ch = curl_init('https://api.twitch.tv/helix/games?name=Minecraft')
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Client-ID: ' . $clientId
    ));
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($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.