Skip to content
PHP Logo

How can the Twitch API be utilized to check if a stream is offline and adjust the display on a website accordingly?

To check if a Twitch stream is offline and adjust the display on a website accordingly, you can utilize the Twitch API to make a request to the stream endpoint and check the stream status. If the stream is offline, you can update the display on your website to show a message indicating that the stream is currently offline.

php
<?php
$channel = "your_channel_name_here";
$client_id = "your_twitch_client_id_here";

$url = "https://api.twitch.tv/kraken/streams/" . $channel . "?client_id=" . $client_id;
$response = file_get_contents($url);
$data = json_decode($response, true);

if($data['stream'] == null) {
    echo "Stream is currently offline";
} else {
    echo "Stream is currently live";
}
?>