<?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'];
?>
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
So
https://api.twitch.tv/helix/streams?first=1&game_id=123&language=de
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!
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!
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;
I get this error:
array(3) { ["error"]=> string(11) "Bad Request" ["status"]=> int(400) ["message"]=> 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.
<?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';
}
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.