final working code
<?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",
"digitizedperson",
);
$found = false;
foreach ($names as $name) {
$url = "https://api.twitch.tv/helix/streams?user_login=" . $name;
$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) {
if (!empty($result->data)) {
if ($result->data[0]->type == 'live') {
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>';
$found = true;
break;
}
}
// break;
} else {
echo 'Failed to decode JSON';
}
} else {
echo 'Failed with: "' . $info['http_code'] > '"';
}
}
if (!$found) {
echo '
<div style="height: 100vh; width: 100%; position: relative;">
<div style="margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);"><img src="https://clicgo.ru/twitch/White-Anthropomorphic-Cat.svg" width="250" alt=""><center><p style="color:white">Strean not found <br></p></center></div>
</div>
';
}
?>