# Webhook twitch start

**URL:** <https://discuss.dev.twitch.com/t/webhook-twitch-start/21010>\
**Category:** API\
**Created:** [April 27, 2019, 1:02am UTC](https://discuss.dev.twitch.com/t/webhook-twitch-start/21010 "2019-04-27T01:02:53Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [April 28, 2019, 10:19am UTC](https://discuss.dev.twitch.com/t/webhook-twitch-start/21010/4 "2019-04-28T10:19:27Z")

</div>

> [@Wed](#):
>
> and I don’t know how to respond to that message. Made an echo, but it doesn’t work.

> [@Got no requests of twitch webhook after subscription](https://discuss.dev.twitch.com/t/got-no-requests-of-twitch-webhook-after-subscription/14993/2):
>
> Did you see the incoming post request from Twitch in your web server access logs? Did you see an error in your incoming server error logs? Did you reply to the incoming hub\_challenge with the hub\_challenge? \<?php if ($\_GET['hub\_challenge']) { echo $\_GET['hub\_challenge']; exit; }

```
if ($_GET['hub_challenge']) {
    echo $_GET['hub_challenge'];
    exit;
}

```

Is correct. (Or the isset you have is _more_ correct)

> [@Wed](#):
>
> file\_put\_contents(“getRequest.txt”, $printString);

You can improve this/save a loop with

> file\_put\_contents(‘getRequest.txt’, print\_r($\_GET,true) . “\n”);

If you need to log $\_GET/$\_POST

> [@Wed](#):
>
> All actions, subscriptions, the beginning of the stream, do not work. What am I doing wrong?

You have subscribed to the follows topic for a user following someone, this will only send you events when `$target_user_id` follows someone else on Twitch (which is back to front for overlay/chat announcments for new followers to a channel).

It won’t send you subscriptions (there is currently no topic for that) and it won’t send you stream changes.

**For Stream Change** Subscribe to `https://api.twitch.tv/helix/streams?user_id=<USERID>`  
**For new follows to a streamer** Subscribe to `https://api.twitch.tv/helix/users/follows?first=1&to_id=<USERID>`  
**For new follows a user makes** Subscribe to `https://api.twitch.tv/helix/users/follows?first=1&from_id=<USERID>`

You probably don’y need the last topic, but that is what you have subscribed to

> [@Wed](#):
>
> ini\_set(“allow\_url\_fopen”, true);

You don’t need and should never enable this on production.

You also don’t need the access control header in this context.

**Additionally** you can use the following:

> **[Reference](https://dev.twitch.tv/docs/api/reference/#get-webhook-subscriptions)**
>
> Twitch Developer tools and services to integrate Twitch into your development or create interactive experience on twitch.tv.

To check the status and existence of any active Webhooks.

**GOTCHA** As this is a webhook following the spec, data is not delivered via `$_POST` but via `php://input` as per

> **[Handling Incoming Webhooks in PHP](https://lornajane.net/posts/2017/handling-incoming-webhooks-in-php)**
>
> An increasing number of applications now offer webhooks as an integration, often in addition to an API. The classic example, familiar to most developers, is the GitHub webhooks which can notify your other systems such as CI tooling that a new commit...

**Code Tidy/corrections**

```
<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    file_put_contents( __DIR__. "/payload.txt", print_r(file_get_contents('php://input'), true) . "\n", FILE_APPEND);

    $incoming_payload = json_decode(file_get_contents('php://input'));
    if (json_last_error() == JSON_ERROR_NONE) {
        // all good do stuff

        header('HTTP/1.1 200 OK');
        echo 'OK';
        exit;
    }

    // someone posted but no data to read?
    header('HTTP/1.1 404 Not Found');
    exit;
}

// must be a GET (should not be PUT/etc

file_put_contents( __DIR__. "/getRequest.txt", print_r($_GET, true) . "\n", FILE_APPEND);
   
if (isset($_GET['hub_challenge'])) {
    $challenge = $_GET['hub_challenge'];
    header('HTTP/1.1 200 OK');
    echo $challenge;
    exit;
}
header('HTTP/1.1 404 Not Found');
exit;

```

Finally `if ($_SERVER['REQUEST_METHOD'] === 'POST') {` is fine. I usually prefer `if ($_POST)` but that’s a preference thing really

---

_[View the full topic](https://discuss.dev.twitch.com/t/webhook-twitch-start/21010)._
