I’m not sure what I am doing wrong. I added my client-id and token but I still cant get this to work.
I am so unsure how to use the API anymore and don’t fully understand what’s going on. Can someone please help me fix this?
<?php
$names[] = "monstercat";
$status = [];
foreach ($names as $name) {
$status[$name] = false;
}
$url = "https://api.twitch.tv/helix/streams?user_login=" . implode('&user_login=', $names);
$client_id = "XXXXXXXXXXXX";
$token = "XXXXXXXXXXXX";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Client-ID: ' . $client_id,
'Authorisation: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] == 200) {
$result = json_decode($result);
if (json_last_error() == JSON_ERROR_NONE) {
foreach ($result->data as $stream) {
// do stuff with stream
$name = strtolower($stream->user_name);
$status[$name] = true;
}
} else {
echo 'Failed to decode JSON';
}
} else {
echo 'Failed with ' . $info['http_code'];
}
foreach ($status as $name => $state) {
if ($state) {
echo '<iframe src="https://player.twitch.tv/?channel=' . $name . '" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>';
} else {
echo $name . ' not live';
}
}
?>
Code Updated.