# Notifications using Twitch API and PHP

**URL:** <https://discuss.dev.twitch.com/t/notifications-using-twitch-api-and-php/1009>\
**Category:** API\
**Created:** [July 20, 2014, 9:37am UTC](https://discuss.dev.twitch.com/t/notifications-using-twitch-api-and-php/1009 "2014-07-20T09:37:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickael\_bonfill](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/mickael_bonfill/32/154_2.png) [@mickael\_bonfill](https://discuss.dev.twitch.com/u/mickael_bonfill)\
**Post date:** [July 20, 2014, 9:37am UTC](https://discuss.dev.twitch.com/t/notifications-using-twitch-api-and-php/1009/1 "2014-07-20T09:37:50Z")

</div>

I’m currently creating a Twitch notification web app in PHP.

For the moment, the authentication works fine :

Client wants use the app -\> redirected on Twitch website to accept asked scopes -\> redirected on my app website with a “code” parameter -\> “code” sent by curl request -\> response containing the accessToken.

I stored accessToken and client username in $\_COOKIE and $\_SESSION variable (set $\_COOKIE doesn’t work on all web browsers).

After that I have to check (in real-time ?) if the user has new followers. Certains web applications, as TNotifier, exists and do this very well… But I don’t know how.

In the Twitch API, we have only for the follows request the possibility to list all of these followers. I thought directly i will have to make requests, again and again (with one second of delay), and compare the new request with the last one… But i think there 's an other way to make that ?

Here is my code, but still thinking a better way exists…

```
$uknown=""; //to initialize my loop function.

comparaison($uknown);

function comparaison($u){
	$options = array(
		'http' => array(
			'header' => 'Accept: application/vnd.twitchtv.v2+json',
			'method' => 'GET',
		)
	);
	$context = stream_context_create($options);
	$result = file_get_contents('https://api.twitch.tv/kraken/channels/test_user1/follows', false, $context);
	$decode_result = json_decode($result, true);
	
	$follow=$decode_result['follows'][0];
	$user=$follow['user'];
	$last_follower=$user['display_name'];
	
	if($last_follower != $u){
		haveANewFollower($last_follower);
	}
	
	comparaison($last_follower);
}

```

Is it possible TNotifier use another way to check new followers ?

---

<div class="post-metadata">

**Author:** ![Fugiman](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/fugiman/32/108_2.png) [@Fugiman](https://discuss.dev.twitch.com/u/Fugiman)\
**Post date:** [July 21, 2014, 7:29pm UTC](https://discuss.dev.twitch.com/t/notifications-using-twitch-api-and-php/1009/2 "2014-07-21T19:29:33Z")

</div>

Currently the API only supports polling like you are doing. However, you should NOT use one second as your delay. That page is cached for five minutes, so there’s no use polling faster than that.

---

<div class="post-metadata">

**Author:** ![mickael\_bonfill](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/mickael_bonfill/32/154_2.png) [@mickael\_bonfill](https://discuss.dev.twitch.com/u/mickael_bonfill)\
**Post date:** [July 22, 2014, 4:31am UTC](https://discuss.dev.twitch.com/t/notifications-using-twitch-api-and-php/1009/3 "2014-07-22T04:31:14Z")

</div>

I finally use my polling diffrently, using ajax which call my php function to make curl request… So in my setTimeOut() function I have to use a kind of value like 3000000 for the delay ? I store new followers in a sql database and notify on my dashboard one by one, so it’s not a problem with a bigger delay.

But if I could suggest you one thing, it’s to integrate the notification in API. I’m using Paypal API for stream donation, and inside it there’s a system called IPN. So after the donator filling the form, he’s redirected to twitch, and a json message is sent to “notify\_url” page (a php page which receives informations and send a code to paypal to confirm it’s not a fraud).  
This json message could be made for twitch API, the user could define his own notify\_url and he wll receive informations instantly. The “notify mode” will be only use by people who activate it, so there’s not much ressources asked to send it I think.

---

<div class="post-metadata">

**Author:** ![Fugiman](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/fugiman/32/108_2.png) [@Fugiman](https://discuss.dev.twitch.com/u/Fugiman)\
**Post date:** [July 22, 2014, 6:01am UTC](https://discuss.dev.twitch.com/t/notifications-using-twitch-api-and-php/1009/4 "2014-07-22T06:01:03Z")

</div>

The functionality you are requesting are commonly known as webhooks and have been requested before. We have no plans at this time to implement webhooks.

---

<div class="post-metadata">

**Author:** ![mickael\_bonfill](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/mickael_bonfill/32/154_2.png) [@mickael\_bonfill](https://discuss.dev.twitch.com/u/mickael_bonfill)\
**Post date:** [July 24, 2014, 2:37pm UTC](https://discuss.dev.twitch.com/t/notifications-using-twitch-api-and-php/1009/5 "2014-07-24T14:37:57Z")

</div>

Thank you for your answer. But I’m thinking about IRC part of the API… Can we know who follow or subscribe with the IRC API in real time ? I ask it because in bot website, like moobot, we canview in real time who is following and subscribe…

---

<div class="post-metadata">

**Author:** ![Fugiman](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/fugiman/32/108_2.png) [@Fugiman](https://discuss.dev.twitch.com/u/Fugiman)\
**Post date:** [July 24, 2014, 11:24pm UTC](https://discuss.dev.twitch.com/t/notifications-using-twitch-api-and-php/1009/6 "2014-07-24T23:24:15Z")

</div>

IRC will let you know who subscribes in real-time, but not who follows. I don’t know the details on how moobot works.

---

<div class="post-metadata">

**Author:** ![FCarentz](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@FCarentz](https://discuss.dev.twitch.com/u/FCarentz)\
**Post date:** [September 19, 2016, 9:35pm UTC](https://discuss.dev.twitch.com/t/notifications-using-twitch-api-and-php/1009/7 "2016-09-19T21:35:33Z")

</div>

I would assume Moobot is using this channel follows from the API

[GET /channels/:channel/follows](https://github.com/justintv/Twitch-API/blob/master/v3_resources/follows.md#get-channelschannelfollows)

When it joins, call that and keep track of the names. Then poll again at a set interval and any new names are new subscribers. Voila.
