Webhook twitch start

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

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

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

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

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:

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

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

1 Like