# Check if a streamer live on twitch (PHP)

**URL:** <https://discuss.dev.twitch.com/t/check-if-a-streamer-live-on-twitch-php/15535>\
**Category:** API\
**Created:** [April 29, 2018, 6:28pm UTC](https://discuss.dev.twitch.com/t/check-if-a-streamer-live-on-twitch-php/15535 "2018-04-29T18:28:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AGuy](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@AGuy](https://discuss.dev.twitch.com/u/AGuy)\
**Post date:** [April 29, 2018, 6:28pm UTC](https://discuss.dev.twitch.com/t/check-if-a-streamer-live-on-twitch-php/15535/1 "2018-04-29T18:28:36Z")

</div>

I use this code

```
<?php
				$streamer = 'Test123';
				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;
				}
				if(is_channel_live($streamer))
					echo "Is streaming";
				else
					echo "Is not streaming";
			?>

```

But it doesnt work  
Notice: Trying to get property of non-object …

---

<div class="post-metadata">

**Author:** ![DDrew](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@DDrew](https://discuss.dev.twitch.com/u/DDrew)\
**Post date:** [April 29, 2018, 9:12pm UTC](https://discuss.dev.twitch.com/t/check-if-a-streamer-live-on-twitch-php/15535/2 "2018-04-29T21:12:48Z")

</div>

You need to send a client ID with your requests or else it will return `error: bad request; status: 400; message: No client id specified`

You can get a client ID by registering an application at [https://dev.twitch.tv/dashboard/apps](https://dev.twitch.tv/dashboard/apps)

Also, just a tip. It’s best to use cURL instead of `file_get_contents` because `file_get_contents` doesn’t return any errors as cURL would.

---

<div class="post-metadata">

**Author:** ![AGuy](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@AGuy](https://discuss.dev.twitch.com/u/AGuy)\
**Post date:** [April 29, 2018, 9:57pm UTC](https://discuss.dev.twitch.com/t/check-if-a-streamer-live-on-twitch-php/15535/3 "2018-04-29T21:57:42Z")

</div>

Thank u

Can you give me cURL example to do this?

---

<div class="post-metadata">

**Author:** ![DDrew](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@DDrew](https://discuss.dev.twitch.com/u/DDrew)\
**Post date:** [April 29, 2018, 10:41pm UTC](https://discuss.dev.twitch.com/t/check-if-a-streamer-live-on-twitch-php/15535/4 "2018-04-29T22:41:43Z")

</div>

Check out this post by /u/matt\_thomas on how to replace file\_get\_contents with cURL.

> [@File\_get\_contents lead time if too long](https://discuss.dev.twitch.com/t/file-get-contents-lead-time-if-too-long/7292/3):
>
> And here is an example of how to create a php function to replace file\_get\_contents: \<?php $clientID = array( ‘Client-ID: 0000000000000000000000000000000’ ); 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, $clientID); …

---

<div class="post-metadata">

**Author:** ![matt\_thomas](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/matt_thomas/32/710_2.png) [@matt\_thomas](https://discuss.dev.twitch.com/u/matt_thomas)\
**Post date:** [April 29, 2018, 10:47pm UTC](https://discuss.dev.twitch.com/t/check-if-a-streamer-live-on-twitch-php/15535/5 "2018-04-29T22:47:31Z")

</div>

I have added the API version in my cURL headers. See below:

> \<?php

> /\* file\_get\_contents replaced with curl function - Add this function and replace any use of file\_get\_contents with file\_get\_contents\_curl \*/
> 
> function file\_get\_contents\_curl($url) {  
> $curlHeader = array(  
> “Client-ID: 352ei7jf3jq2mu6jvvovjy4qwv6huc”, /\* SET CLIENT ID HERE \*/  
> “Accept: application/vnd.twitchtv.v5+json”  
> );  
> $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, $curlHeader);  
> $data = curl\_exec($ch);  
> curl\_close($ch);  
> return $data;  
> }

> ?\>

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [May 29, 2018, 10:47pm UTC](https://discuss.dev.twitch.com/t/check-if-a-streamer-live-on-twitch-php/15535/6 "2018-05-29T22:47:40Z")

</div>

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