I hope it is okay that I reply on this topic instead of a new one.
I want to do the same thing as PepeMax and display it on our dashboard. We basically have a Twitch Casting Team which should be displayed on the front page of us if they stream. Because the embedding does not show the host, we used the code of matt_thomas. I tried everything but it just doesn’t want to work. It doesn’t show a box and no active hosts. Anyone able to help?
It should be displayed here under our banner -> https://arctic-wolves.org/
we added the clientID and the streamers in the code but as you see, on our website it says only “Streamer:” and shows no streaming box, nothing.
Code:
Summary
[html]
<html>
<head>
<title>TEST</title>
</head>
<body>
<?php
/* Mucked together by Matt_Thomas @ Twitch.tv
revised version of http://twitch.apiexampl.es/teams.php.txt - designed for a group of streamers who are not a team yet, can manually create a list below..
created for PepeMax on https://staging-discuss.dev.twitch.com/t/integration-of-a-live-hosting-on-a-website/25120/8
Needs some sort of cachine for production use.
matt AT isso DOT pro */
/* file_get_contents replaced with curl function */
function file_get_contents_curl($url)
{
$curlHeader = array("Client-ID: id1ba10gzooqcd0gp31t9nhh3gd6pc", "Accept: application/vnd.twitchtv.v5+json"); /* SET CLIENT ID HERE */
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeader);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$channels = array("b1kaygaming", "foosel20_de"); /* For now, we use a manually set list of streamers */
$team = implode("&login=",$channels); /* Clean our list for next step */
$teamData = json_decode(@file_get_contents_curl('https://api.twitch.tv/helix/users?login=' . $team), true);
$teamMembersID = array();
if ($teamData['data'] != null) { /* Make sure we get a response from the API */
foreach ($teamData['data'] as $mydata) { /* Build an array with online streamers from our list */
if ($mydata['id'] != null) { /* Make sure the streamer has data in the API */
array_push($teamMembersID, $mydata['id']); /* Store the IDs */
}
}
$callAPI = implode(",", $teamMembersID); /* Clean our list for next step */
$dataArray = json_decode(@file_get_contents_curl('https://api.twitch.tv/kraken/streams?limit=1&channel=' . $callAPI), true); /* Take the auto populated team list and pick the top streamer */
$name = $dataArray['streams']['0']['channel']['name'];
} else { /* None of the $channels I wanted are avaiable now, so fetch the most popular one */
$backupArray = json_decode(@file_get_contents_curl('https://api.twitch.tv/kraken/streams?limit=1' . $callAPI), true);
if ($backupArray != null) {
foreach ($backupArray['streams'] as $mydata) {
if ($mydata['_id'] != null) {
$name = $mydata['channel']['display_name'];
$viewers = $mydata['viewers'];
} else {
echo "Error in results from api.twitch.tv, cannot fetch a channel."; /* API responded but not with expected data */
}
}
} else {
echo "Error in results from api.twitch.tv, probably cannot connect to server."; /* API probably did not respond at all */
}
}
?>
<center>
<h1> Streamer: <?php echo "$name"; ?> </h1>
<iframe src="http://player.twitch.tv/?channel=<?php echo "$name"; ?>" height="720" width="1280" frameborder="0" scrolling="no" allowfullscreen="true"></iframe>
</center>
</body>
</html>
[/html]