API Twitch check if a stream is online

0

I have some problem to create an extension, I need to know if a twitch channel is online.

A friend helped me, since in the markup language I'm null, with this php.

But I do not know if it can be applied to the web.

<?php
$streamChannel = "mastersonothing";  
$url = "https://api.twitch.tv/kraken/streams?channel=mastersonothing"
$clientid = "12345678901234"

$json_array = json_decode(curl($url), true);  

$newurl = $url . "&client_id=" . $clientid;

if(isset($json_array['streams'][0]['channel'])) {  
echo "<div id='streamonline'>Stream is Online</div>";  
} else {  
echo "<div id='streamoffline'>Stream is Offline</div>";  
}  

?>

The problem is that I do not know if it can be used equally in js, and in turn, it would work correctly, could someone help me to do this simple check?

    
asked by Pablo Casillas García 12.04.2018 в 22:49
source

1 answer

0

In the one of documentation Twitch, it says how to do it, I leave you the code to make the call with Jquery .

Two parameters are required: the client-id and the chanlel-name which is the same as the username.

var endpoint = "https://api.twitch.tv/kraken/streams/";
var channelname= "c9sneaky";

$.ajax({
 type: 'GET',
 url: endpoint + channelname,
 headers: {
   'Client-ID': clientid
 },
 success: function(data) {
   console.log(data);
 }
});

Depending on whether it is live or not, it returns a type of JSON or another.

In case is live :

{
    "stream": {
        "_id": 28311073152,
        "game": "League of Legends",
        "viewers": 19530,
        "video_height": 1080,
        "average_fps": 60,
        "delay": 0,
        "created_at": "2018-04-13T03:05:36Z",
        "is_playlist": false,
        "stream_type": "live",
        "preview": {
            "small": "https://static-cdn.jtvnw.net/previews-ttv/live_user_c9sneaky-80x45.jpg",
            "medium": "https://static-cdn.jtvnw.net/previews-ttv/live_user_c9sneaky-320x180.jpg",
            "large": "https://static-cdn.jtvnw.net/previews-ttv/live_user_c9sneaky-640x360.jpg",
            "template": "https://static-cdn.jtvnw.net/previews-ttv/live_user_c9sneaky-{width}x{height}.jpg"
        },
        "channel": {
            "mature": false,
            "partner": true,
            "status": "COGGERS (Twitch Prime)",
            "broadcaster_language": "en",
            "display_name": "C9Sneaky",
            "game": "League of Legends",
            "language": "en",
            "_id": 24538518,
            "name": "c9sneaky",
            "created_at": "2011-09-02T23:16:11Z",
            "updated_at": "2018-04-13T07:34:08Z",
            "delay": null,
            "logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/c9sneaky-profile_image-03d2c6cbb19c9e5d-300x300.png",
            "banner": null,
            "video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/c9sneaky-channel_offline_image-19723bab4c7f1146-1920x1080.png",
            "background": null,
            "profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/a9c5b05f-e590-4ebd-b52a-4904512e96f9-profile_banner-480.jpg",
            "profile_banner_background_color": "",
            "url": "https://www.twitch.tv/c9sneaky",
            "views": 79363893,
            "followers": 1186853,
            "_links": {
                "self": "https://api.twitch.tv/kraken/channels/c9sneaky",
                "follows": "https://api.twitch.tv/kraken/channels/c9sneaky/follows",
                "commercial": "https://api.twitch.tv/kraken/channels/c9sneaky/commercial",
                "stream_key": "https://api.twitch.tv/kraken/channels/c9sneaky/stream_key",
                "chat": "https://api.twitch.tv/kraken/chat/c9sneaky",
                "features": "https://api.twitch.tv/kraken/channels/c9sneaky/features",
                "subscriptions": "https://api.twitch.tv/kraken/channels/c9sneaky/subscriptions",
                "editors": "https://api.twitch.tv/kraken/channels/c9sneaky/editors",
                "teams": "https://api.twitch.tv/kraken/channels/c9sneaky/teams",
                "videos": "https://api.twitch.tv/kraken/channels/c9sneaky/videos"
            }
        },
        "_links": {
            "self": "https://api.twitch.tv/kraken/streams/c9sneaky"
        }
    },
    "_links": {
        "self": "https://api.twitch.tv/kraken/streams/c9sneaky",
        "channel": "https://api.twitch.tv/kraken/channels/c9sneaky"
    }
}

And in case the channel is not live :

{
    "stream": null,
    "_links": {
        "self": "https://api.twitch.tv/kraken/streams/shiphtur",
        "channel": "https://api.twitch.tv/kraken/channels/shiphtur"
    }
}
    
answered by 13.04.2018 в 09:42