Check if a Streamer is Live (optional PHP)

Hey Guys I read a lot of stuff here how I can check if a streamer is live but its the first time I work with a API so I have many problems with it.

What is the easiest way to check if a streamer is live?
I saw that I can send create a link with the stream name and the client ID but the examples i tried didnt work.

Would be awesome if u can help me out.

Get Streams is documented here

For example

curl -H 'Client-ID: uo6dggojyb8d6soh92zknwmi5ej1q2' \
-X GET 'https://api.twitch.tv/helix/streams?user_login=foo'

Just use a PHP cURL call and a real clientID

My main problem is that I understand the doc in the most parts but I dont know how tow converte this information:

curl -H 'Client-ID: uo6dggojyb8d6soh92zknwmi5ej1q2' \
-X GET 'https://api.twitch.tv/helix/streams?user_login=foo'

to php code.

Thats what I have done yet:

<?php $ClientID = array('Client-ID: uo6dggojyb8d6soh92zknwmi5ej1q2'); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $ClientID); curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/streams?user_login=foo"); curl_close($ch); ?>

In my console there is only one message with:

{“error”:“Bad Request”,“status”:400,“message”:“No client id specified”}

Is an example client ID and not to be used. It’s only for the docs to illustrate how to use a clientID.

You need your own clientID as documented

I know i used my own Client ID. I just copied this example Client Id into it. So i dont publish my.

I created one for the localhost to test and with it and the code I get the same bad request.

<?php

$ch = curl_init('https://api.twitch.tv/helix/streams?user_login=foo');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Client-ID: MYCLIENTID'
));
$r = curl_exec($ch);
curl_close($ch);

print_r($r);

Results in

php test.php
{"data":[],"pagination":{}}

ClientID’s are fine to publish as they are “public”

1 Like

Ok I tried ur code and got now

{“data”:[],“pagination”:{}}

on my site.

Yup that indicates that the streamer “foo” is offline/not live

Allright. Nice if I use a live streamer I got some informations on the site.

That is more then i got after few days of work :sweat_smile:

Is it possible to check more then 1 channel at the same time or did I need for every channel a request?

As per the docs:

user_id string Returns streams broadcast by one or more specified user IDs. You can specify up to 100 IDs.
user_login string Returns streams broadcast by one or more specified user login names. You can specify up to 100 names.

So

https://api.twitch.tv/helix/streams?user_login=bob&user_login=foo&user_login=fred
1 Like

Allright thx.

So if a user is going now on the test.php. It checks with the function above if and which streamer is live and sends one request. But if there are many visitors then it would send many requests to the API. Is this a problem?

Yes. The API has a rate limit.

You should consider setting up a cron job to collect stream status periodically and store that in a database for your website to recall.

Alterantively you can also use Twitch Webhooks to do the same. And do away with the long polling

1 Like

Ok thx man.

You helped me so much.

One more question.

I read the Doc to the rate Limits and the Bearer token but not exactly sure what the Bearer token is.
So if I do a request like above for one streamer is it 1 request out of 30 I can send in a min or 1 out of 800?

And if I do a request with many channels like 20. Is it 1 request out of 30/800 or 20 requests?

Sorry for my questions but I find it hard to understand the Twitch Doc in some points.

1 HTTP request is one request.

1 HTTP request for 20 channels is one request

Ok thx and can I do 30 or 800 request in a min if I only check if the streamer is live with the method above?

The API uses a leaky bucket sort of algorithm.

You get 30 requests per period normally
And 800 if you specify a bearer

But what exactly is the bearer in this case?

Is the client ID a bearer?

Bearer can be a user access token or an app access token