Check If Twitch stream online of offline using PHP

Hello Guys,

I am trying to see if stream of particular user is online or offiline in my PHP and here is my try so far.

<?php 

// Get Live Streams 

function file_get_contents_curl($url) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);

    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(

        'Client-ID: **********************',        

        'Authorization: Bearer **************', 

        'Accept: application/vnd.twitchtv.v5+json'

                

    ));

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;

}

$url = "https://api.twitch.tv/helix/streams";

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

echo '<pre>';

print_r($json_array);

exit;

// 71588578

?>

Here I am using helix as my channel and I am getting response having id,game_id etc in response … but when I am trying to get other channel’s streams for example this url https://api.twitch.tv/emadgg/streams with my this api url $url = “https://api.twitch.tv/emadgg/streams”; , I am not getting any response … its blank in return.

I have also tried with some other channels $url = “https://api.twitch.tv/swagg/streams”; but none of them working for me except for helix channel. Not sure why …

Basically here I want to know if stream is live or not. But its not working. can someon guide me what I am doing wrong here ?

I am following this thread https://dev.twitch.tv/docs/api/reference/#get-streams.

Additionally, I am using Twitch IRC to get live chat results like this Chat bot in php CLI

That’s because that isn’t a valid url. https://api.twitch.tv/helix/streams then use query params for channel name or game id as stated in the docs you linked.

@WLG3R Thanks for your reply.

I have added below url for my channel

$url = "https://api.twitch.tv/helix/streams/?channel=symfuhny";

This is a live stream url at the moment … https://www.twitch.tv/symfuhny … The live stream running at the moment… but the thing is I am getting the same results in response…

[0] => Array
                (
                    [id] => 6214848871696298717
                    [user_id] => 139470326
                    [user_name] => 한동숙
                    [game_id] => 512980
                    [type] => live
                    [title] => 폴 가이즈 우승하러 옴
                    [viewer_count] => 24131
                    [started_at] => 2020-08-05T02:50:18Z
                    [language] => ko
                    [thumbnail_url] => https://static-cdn.jtvnw.net/previews-ttv/live_user_handongsuk-{width}x{height}.jpg
                    [tag_ids] => Array
                        (
                            [0] => ab2975e3-b9ca-4b1a-a93e-fb61a5d5c3a4
                        )

                )

And more data …

I am trying to change url with another live stream for example $url = "https://api.twitch.tv/helix/streams/?channel=nickeh30"; , https://www.twitch.tv/nickeh30 but still i m getting same response …

Bit confused why its happening …

OK Guys, I have made it work using user_ login like following url …

$url = "https://api.twitch.tv/helix/streams/?user_login=akalus";

Hope this helps …

For future reference the documentation for the streams endpoint can be found at

@BarryCarlyon Yea … thank you so much for your help and support. It really helped a lot.
Thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.