Requesting stream info with Client ID in Twitch APIv5

how can I implement my client-ID in my request to the twitch API servers via PHP?

This is my current code:
$clientid=“xxxxxxxxx”;
function is_channel_live($channel)
{
$request = json_decode( @file_get_contents( ‘https://api.twitch.tv/kraken/streams/’ . $channel ) );
return ( ! is_null( $request->stream ) ) ? TRUE : FALSE;
}
$chan = “shroud”;
if (is_channel_live($chan)==true)
echo “Live!!”;

else
echo "Offline..";

How would I actually implement the client ID in the request?

May also note that I am very new to the twitch API…

There is a PHP example in the Client-ID blog post. See if that helps!

1 Like

I would def recommend using a cURL library like in the example that @DallasNChains recommended. If you don’t want to use cURL directly, there are several good third party libraries out there (Guzzle & Unirest, to name a couple off the top of my head). They will allow you to easily parse headers so you can do quick checks for 200 and 404, rather than just suppressing any errors that come up retrieving the data.

But if you absolutely insist on using file_get_contents, you need to include the API version and client id inline in the URL like this (79462105 is my channel, so don’t copy and paste verbatim):

https://api.twitch.tv/kraken/streams/79462105?client_id=xxxxxxxxxx&api_version=5

And don’t forget to have the api_version=5 in your query parameters as the default is still v3.

Hope this helps

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