Vorrei aggiungere uno scpript ad un login

Vorrei aggiungere questo script per verificare se l’utente è abbonato:

function IsSubscribed( $AT, $username, $channel ) {
        
        $h = [
            'Client-ID: ' . $this->client_id,
            'Authorization: OAuth ' . $AT
        ];
        $sub = $this->get( 'users/' . $username . '/subscriptions/' . $channel, $h );
        if( isset( $sub['created_at'] ) ) {
            return 100;
        } else {
            return $sub['status'];
        }
        
    }

questo script lo ho preso da una libreira github, ora vorrei aggiungerlo al mio script di login per verificare se l’utente che fa l’accesso è abbonato oppure no, in questo caso mandarlo alla index con echo che dice di abbonarsi per fare il login, se l’utente è abbonato lo manda al mio “redirect_uri”. Questo è lo script di login:

<?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';
}

qualcuno potrebbe aiutarmi per farlo funzionare? Grazie.

Fixed your post formatting for you

Change

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

            echo '<p>Thanks ' . $user->data[0]->display_name . ' <3</p>';
        } else {

to include a subscriptions check.

So you complete the login then perform the subscription check. You’ll beed a broadcaster token to do the subscription check

I’m not sure what your IsSubscribed will do as it seems to be part of a class and is missing code.

============================

Risolto il problema con la formattazione del tuo post

Modificare

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

            echo '<p>Thanks ' . $user->data[0]->display_name . ' <3</p>';
        } else {

per includere un controllo degli abbonamenti.

Quindi completi il login quindi esegui il controllo dell’abbonamento. Ti verrà assegnato un token di emittente per eseguire il controllo dell’abbonamento

Non sono sicuro di cosa farà il tuo “IsSubscripts” dato che sembra essere parte di una classe e manca del codice.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.