PHP EventSubs Examples

Thanks for you help…

I’ve tried so far your example. And one where I fetch step by step the tokens with different scopes. This works fine.

BUT… also with different eventsubs I always get the error:

“Got tokenfailed to create sub: {“error”:“Bad Request”,“status”:400,“message”:“unsupported eventsub transport method”}”

<?php

include(__DIR__ . '/config.php');

$ch = curl_init('https://id.twitch.tv/oauth2/token?client_id=' . CLIENT_ID . '&client_secret=' . CLIENT_SECRET . '&grant_type=client_credentials');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$r = curl_exec($ch);
$i = curl_getinfo($ch);
curl_close($ch);

if ($i['http_code'] == 200) {
    $keys= json_decode($r);
    if (json_last_error() == JSON_ERROR_NONE) {
        echo 'Got token';


        $ch = curl_init('https://api.twitch.tv/helix/eventsub/subscriptions');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
            
			   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
            'type' => 'channel.subscribe"',
            'version' => '1',
            'condition' => array( 'broadcaster_user_id' => 'myuserID' ),
            'transport' => array(
                'method' => 'webhook',
                'callback' => 'https://mywebsite.com/twitch/recieveEvents.php',
                'secret' => '23k4hadfjd'
				)
			)))
			
        )));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Client-ID: ' . CLIENT_ID,
            'Authorization: Bearer ' . $keys->access_token
        ));

        $r = curl_exec($ch);
        $i = curl_getinfo($ch);
        curl_close($ch);

        if ($i['http_code'] == 200) {
            // created sub OK
           echo 'created sub OK';
        } else {
           echo 'failed to create sub: ' . $r;
        }
    } else {
        echo 'Failed to parse JSON';
    }
} else {
    echo 'Failed with ' . $i['http_code'] . ' ' . $r;
}

I want to get event when someone redeemed channel points so I tried

{
    "type": "channel.channel_points_custom_reward.update",
    "version": "1",
    "condition": {
        "broadcaster_user_id": "myuserid",
        "reward_id": "9001" // optional to only get notifications for a specific reward
    },
    "transport": {
        "method": "webhook",
        'callback' => 'https://mywebsite.com/twitch/recieveEvents.php',
        "secret": "s3cRe7"
    }
}

(Edit: userid and callback url are just placeholder…I’ve tried it with my own data :slight_smile: )

Thanks for your thoughts on this
Greetings

ps.: u need a Ko-Fi account!!!