Update Twitch Channel

Here is a none tested, not to be used in production, one pager example that should work.

<?php

$client_id = '';
$client_secret = '';
$redirect_uri = 'https://mywebsite.com/path/to/this/page/';

session_start();

if (isset($_GET['logout']) && $_GET['logout']) {
    session_destroy();

    header('Location: /path/to/this/page/');
} else if (isset($_GET['code']) && $_GET['code']) {
    $curl = curl_init('https://id.twitch.tv/oauth2/token');
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, array(
        'client_id' => $client_id,
        'client_secret' => $client_secret,
        'grant_type' => 'authorization_code',
        'redirect_uri' => $redirect_uri,
        'code' => $_GET['code']
    ));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($curl);
    $i = curl_getinfo($curl);
    curl_close($curl);

    if ($i['http_code'] == 200) {
        $result = json_decode($result, true);

        $_SESSION['twitch'] = $result;

        $curl = curl_init('https://id.twitch.tv/oauth2/validate');
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Authorization: OAuth ' . $_SESSION['twitch']['access_token']
        ));
        $user = curl_exec($curl);
        $i = curl_getinfo($curl);
        curl_close($curl);

        if ($i['http_code'] == 200) {
            $validate = json_decode($user);

            $_SESSION['twitch_id'] = $validate->user_id;

            header('Location: /path/to/this/page/');
        } else {
            echo '<p>An Error Occucred please try again</p>';
        }
    } else {
        echo '<p>An Error Occucred please try again</p>';
    }
} else if (isset($_SESSION) && isset($_SESSION['twitch']) && $_SESSION['twitch']) {
    if (isset($_POST) && $_POST) {
        $curl = curl_init('https://api.twitch.tv/kraken/channels/' . $_SESSION['twitch_id']);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
            'status' => $_POST['status'],
            'game' => $_POST['game']
        ]));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Authorization: OAuth ' . $_SESSION['twitch']['access_token'],
            'Content-Type: application/json'
        ));

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

        if ($i['http_code'] == 200) {
            echo 'OK';
        } else {
            echo 'An Error Occured';
        }
    } 
    ?>
<a href="?logout=1">Logout</a>
<form action="" method="post">
    <fieldset>
        <div>
            <label for="game">Game</label>
            <input type="text" name="game" id="game" />
        </div>
        <div>
            <label for="status">Title</label>
            <input type="text" name="status" id="status" />
        </div>
        <div>
            <input type="submit" />
        </div>
    </fieldset>
</form>
    <?php
} else {
    ?>

<p>You need to login with Twitch</p>
<a href="https://id.twitch.tv/oauth2/authorize?client_id=<?php echo $client_id; ?>&redirect_uri=<?php echo $redirect_uri; ?>&response_type=code&scope=">Login</a>

    <?php
}

Some References
https://lornajane.net/posts/2009/putting-data-fields-with-php-curl

https://www.php.net/manual/en/function.curl-exec.php

I’d suggest learning the basics of PHP before trying to do something advanced such as this.

1 Like