Login php sul mio sito web

Hi, I am recently a web programmer, I am creating a site with a login system with twitch for the subscribers of a channel, each level of subscription has different advantages so I wanted to create a login in php that would send users of a certain subscription level on a different page to that of another level, then for the moderators a different page and the same thing for the VIPs, how do I do it? is anyone kind enough to send me a working code?

Authentication is covered here

You’ll most likely use

You will need a a token from the broadcaster for checking subscriber status or moderator status
VIP status is not in the API so you’ll have to extract that from chat or use another system

You would also use the same flow for user login

is an example that demonstrates this method in PHP. The tool lets you generate a token with any scope applied to it for example purposes

Thanks for the answer, the problem is that being inexperienced I don’t know how to actually create a complete script and on the documentation I can’t find it, can someone send here a basic example? Thanks

Err I linked to one in the GitHub repo for PHP…

what?

I have this code, okay?

<?php
$client_id = ‘’;
$client_secret = ‘’;
$redirect_uri = ‘’;
$check = $_GET[‘code’];

if ($check != null) {
$token_url = "https://api.twitch.tv/kraken/oauth2/token 15";
$data = array(
‘client_id’ => $client_id,
‘client_secret’ => $client_secret,
‘grant_type’ => ‘authorization_code’,
‘redirect_uri’ => $redirect_uri,
‘code’ => $_GET[‘code’]
);

$curl = curl_init($token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1000);

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

if ($i['http_code'] == 200) {
    $result = json_decode($result, true);
    
    // get
    $curl = curl_init('https://api.twitch.tv/kraken/user');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Accept: application/vnd.twitchtv.v3+json',
        'Client-ID: ' . $client_id,
        'Authorization: OAuth ' . $result['access_token']
    ));
    curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
	
    $user = curl_exec($curl);
    $i    = curl_getinfo($curl);
    curl_close($curl);
    
    if ($i['http_code'] == 200) {
        $user = json_decode($user);
        
        echo '<p>Thanks ' . $user->display_name . ' <3</p>';
    } else {
        echo '<p>An error occured, please <a href="/index.php">click here and try again</a></p>';
    }
} else {
    echo '<p>An error occured, please <a href="/index.php">click here and try again</a></p>';
}
} else {
$scopes = array(
‘user_read’ => 1
);

$req_scope = '';
foreach ($scopes as $scope => $allow) {
    if ($allow) {
        $req_scope .= $scope . '+';
    }
}
$req_scope = substr($req_scope, 0, -1);

$auth_url = 'https://api.twitch.tv/kraken/oauth2/authorize?response_type=code';
$auth_url .= '&client_id=' . $client_id;
$auth_url .= '&redirect_uri=' . $redirect_uri;
$auth_url .= '&scope=' . $req_scope;
$auth_url .= '&force_verify=true';

echo '<a href="' . $auth_url . '">Please Click this Link to Authenticate with Twitch</a>';
}

The GitHub repo I linked to

Provide a basic example on how to generate a user access token.

It also provides a script that tells you how to maintain that token.

is login?

Needs to be

$token_url = "https://api.twitch.tv/oauth2/token";

But seems correct

thanks

sorry again, it gives me this error: {“status”: 400, “message”: “invalid scope requested: ‘’ user_read ‘’”}
what string is it in?

I missed this

'Accept: application/vnd.twitchtv.v3+json',

Is wrong you seem to be using an outdated reference and not my GitHub repo

Also what is the logjn URL you generated? So we can check the syntax of it

what’s wrong with the code?

v3 is dead…

Requires a v5 header. And you specified a v3 header.

So you seem to be following a really old guide and not my example which uses helix instead of the deprecated kraken

so I have to change only the link or everything?

Here I tried to modify your script you are using to something that might work, have not tested it.


<?php

$client_id = '';
$client_secret = '';
$redirect_uri = '';

if (isset($_GET['code']) && $_GET['code']) {
    $token_url = "https://id.twitch.tv/oauth2/token";
    $data = array(
        'client_id' => $client_id,
        'client_secret' => $client_secret,
        'grant_type' => 'authorization_code',
        'redirect_uri' => $redirect_uri,
        'code' => $_GET['code']
    );

    $curl = curl_init($token_url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 1000);

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

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

        $curl = curl_init('https://api.twitch.tv/helix/user');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Client-ID: ' . $client_id,
            'Authorization: Bearer ' . $result['access_token']
        ));
        curl_setopt($curl, CURLOPT_TIMEOUT, 1000);

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

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

            echo '<p>Thanks ' . $user->data[0]->display_name . ' <3</p>';
        } else {
            echo '<p>An error occured, please <a href="/index.php">click here and try again</a></p>';
        }
    } else {
        echo '<p>An error occured, please <a href="/index.php">click here and try again</a></p>';
    }
} else {
    $auth_url = 'https://api.twitch.tv/kraken/oauth2/authorize?response_type=code';
    $auth_url .= '&client_id=' . $client_id;
    $auth_url .= '&redirect_uri=' . $redirect_uri;
    $auth_url .= '&scope=user_read';
    $auth_url .= '&force_verify=true';

    echo '<a href="' . $auth_url . '">Please Click this Link to Authenticate with Twitch</a>';
}

she is a genius! sorry again, now what do I have to add to send users with a certain level of subscription to different pages?

thanks, i just don’t know how to insert it

First you get a token for the broadcaster, like you did for logging a user in.

Then you make a call to the subscribers API the same way you are already making calls to the users API

you take

        $curl = curl_init('https://api.twitch.tv/helix/user');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Client-ID: ' . $client_id,
            'Authorization: Bearer ' . $result['access_token']
        ));
        curl_setopt($curl, CURLOPT_TIMEOUT, 1000);

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

and substitute the variables

        $curl = curl_init('https://api.twitch.tv/helix/subscriptions?broadcaster_id=' . $broadcaster_id . '&user_id' . $user_id);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Client-ID: ' . $client_id,
            'Authorization: Bearer ' . $broadcasters_token
        ));
        curl_setopt($curl, CURLOPT_TIMEOUT, 1000);

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