<?php
$client_id = "************";
$client_secret = "*******";
$link = "https://id.twitch.tv/oauth2/token";
$data = "client_id=" . $client_id . "&client_secret=" . $client_secret . "&grant_type=client_credentials";
// Request cURL POST pour get le token
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
curl_close($ch);
// Decode
$token = json_decode($res);
$names = Array("agentwxo", "welovegames");
$status = [];
foreach ($names as $name) {
$status[$name] = false;
}
$url = "https://api.twitch.tv/helix/streams?user_login=" . $names[1];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token->access_token, 'Client-ID: ' . $client_id));
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 . '&parent=clicgo.ru&muted=true"
height="480"
width="720"
allowfullscreen>
</iframe>
<iframe id="twitch-chat-embed"
src="https://www.twitch.tv/embed/' . $name . '/chat?parent=clicgo.ru"
height="480"
width="350">
</iframe>';
} else {
echo $name . ' not live';
}
}
?>
I can not understand where the error is, Visual Studio code does not give an error. And the site doesn’t work.