VOD ID Output? So have implemented this

Hello, wanted to ask if someone can help out, wanted to output 20 VOD IDs.

Here is my Code:

define("CLIENT_ID", "XXX");
define("CLIENT_SECRET", "XXX");

$keys1 = false;
if (file_exists(__DIR__ . '/auth.json')) {
    $keys1 = json_decode(file_get_contents(__DIR__ . '/auth.json'));
}

$generate_token = true;
if ($keys1) {
    // validate the token

    $ch991 = curl_init('https://id.twitch.tv/oauth2/validate');
    curl_setopt($ch991, CURLOPT_HTTPHEADER, array(
        'Authorization: OAuth ' . $keys1->access_token
    ));
    curl_setopt($ch991, CURLOPT_RETURNTRANSFER, true);

    $r1 = curl_exec($ch991);
    $i1 = curl_getinfo($ch991);
    curl_close($ch1);

    if ($i1['http_code'] == 200) {
        // the token appears good
        $generate_token1 = false;

        // optional to check the expires
        $token1 = json_decode($r1);
        if (json_last_error() == JSON_ERROR_NONE) {
            if ($token1->expires_in < 3600) {
                // less than an hour left
                // make a new token
                echo 'Token close to expire. Regenerate';
                $generate_token1 = true;
            }
        } else {
            echo 'Failed to parse JSON. Assume dead token';
            $generate_token1 = true;
        }
    }
}

if ($generate_token1) {
    $ch991 = curl_init('https://id.twitch.tv/oauth2/token');
    curl_setopt($ch991, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch991, CURLOPT_POST, true);
    curl_setopt($ch991, CURLOPT_POSTFIELDS, array(
        'client_id' => CLIENT_ID,
        'client_secret' => CLIENT_SECRET,
        'grant_type' => "client_credentials"
    ));

    $r1 = curl_exec($ch991);
    $i1 = curl_getinfo($ch991);
    curl_close($ch991);

    if ($i1['http_code'] == 200) {
        $keys1 = json_decode($r1);
        if (json_last_error() == JSON_ERROR_NONE) {
            echo 'Got token';
            // store the token for next run
            file_put_contents(__DIR__ . '/auth.json', $r1, JSON_PRETTY_PRINT);
        }
}
}

 $channelsApi312 = 'https://api.twitch.tv/helix/users?login=codeSMART78';
 $clientId = 'XXX';
 $ch312 = curl_init();
 
 curl_setopt_array($ch312, array(
    CURLOPT_HTTPHEADER => array(
        'Client-ID: ' . $clientId,
        'Authorization: Bearer ' . $keys1->access_token
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi312
 ));

 $response312 = curl_exec($ch312);

$resp312 = json_decode($response312);
$name2 = $resp312->data[0]->id;
$first = "&first=20";

 $channelsApi31 = 'https://api.twitch.tv/helix/videos?user_id=';
 $clientId = 'XXX';
 $ch31 = curl_init();
 
 curl_setopt_array($ch31, array(
    CURLOPT_HTTPHEADER => array(
        'Client-ID: ' . $clientId,
        'Authorization: Bearer ' . $keys1->access_token
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi31 . $name2 . $first
 ));

 $response31 = curl_exec($ch31);

$resp31 = json_decode($response31);
$videos1 = $resp31->data[0]->id;
echo $videos1;

I have just one Output: Token OK!

  1. usernames are all lower case, thankfully this normally doesn’t cause an issue
  1. You have no HTTP Code checks or JSON decode checks like the token generation code does.

Did you get a 200?
Did you actually get one user returned?

Same problem for curl 3, no HTTP code checking and no JSON decode checking did you even get any videos returned did the request succeed?

  1. You used two different styles of curl functions, which in itself isn’t a problem just makes it weird when reading your code.

I also personally avoid curl_setopt_array myself but thats my preference.

  1. Why are all your variabels $xnumber

After closing curl on the first request you can reuse the same variable for the next request, may make your code more legible.

This feels like you found my auth token example in PHP and then bastardised it by merged it with another example and then rather than using your existing CLIENT_ID constant, redelcaring your client_id in the code.

Solution

Rather than taking your version and fixing it I wrote a version from scratch using my auth example as a basis https://github.com/BarryCarlyon/twitch_misc/blob/main/authentication/app_access_tokens/php/generate_and_maintain_token.php

Make sure to note that for each curl request there are HTTP code checks and “did JSON parse” checks on each step

<?php

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

$keys = false;
if (file_exists(__DIR__ . '/auth.json')) {
    $keys = json_decode(file_get_contents(__DIR__ . '/auth.json'));
}

$generate_token = true;
if ($keys) {
    // validate the token

    $ch = curl_init('https://id.twitch.tv/oauth2/validate');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: OAuth ' . $keys->access_token
    ));

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

    if ($i['http_code'] == 200) {
        // the token appears good
        $generate_token = false;

        // optional to check the expires
        $data = json_decode($r);
        if (json_last_error() == JSON_ERROR_NONE) {
            if ($data->expires_in < 3600) {
                // less than an hour left
                // make a new token
                echo 'Token close to expire. Regenerate';
                $generate_token = true;
            }
        } else {
            echo 'Failed to parse JSON. Assume dead token';
            $generate_token = true;
        }
    }
}

if ($generate_token) {
    $ch = curl_init('https://id.twitch.tv/oauth2/token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'client_id' => CLIENT_ID,
        'client_secret' => CLIENT_SECRET,
        'grant_type' => "client_credentials"
    ));

    $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';

            // store the token for next run
            file_put_contents(__DIR__ . '/auth.json', $r);
        } else {
            echo 'Failed to parse JSON';
            exit;
        }
    } else {
        echo 'Failed with ' . $i['http_code'] . ' ' . $r;
        exit;
    }
} else {
    echo 'Token OK' . "\n";
}

$ch = curl_init('https://api.twitch.tv/helix/users?login=codesmart78');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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) {
    $users = json_decode($r);
    if (json_last_error() == JSON_ERROR_NONE) {
        // got some data
        if ($users->data && count($users->data) == 1) {
            $user_id = $users->data[0]->id;

            echo 'Getting 20 videos for ' . $user_id . "\n";

            $ch = curl_init('https://api.twitch.tv/helix/videos?user_id=' . $user_id . '&first=20');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            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) {
                $videos = json_decode($r);
                if (json_last_error() == JSON_ERROR_NONE) {
                    // got some data
                    if ($videos->data && count($videos->data) > 0) {
                        $video_ids = [];
                        foreach ($videos->data as $video) {
                            $video_ids[] = $video->id;
                        }

                        echo 'Got Videos'."\n";
                        print_r($video_ids);
                    } else {
                        echo 'Got No vidoes';
                    }
                } else {
                    echo 'Failed to parse JSON for videos';
                }
            } else {
                echo 'Failed with to get any videos ' . $i['http_code'] . ' ' . $r;
            }
        } else {
            echo 'Failed to get a user';
        }
    } else {
        echo 'Failed to parse JSON for user';
    }
} else {
    echo 'Failed with to get user ' . $i['http_code'] . ' ' . $r;
}

Example result

image

Hello und thanks for the Fast Reply, to the Part of basterdized, its mother, hehehe I am joking. The Code works just wonderll. I have the question, how to get the output done so that only pure Video ID without the other comes out so just numbers as the IDs, maby by Breaks, so I want to put them into iframes?

This script isn’t really suitable for that use case.

It’s an example that will demonstrate how to take that and use it for your own means.

To spit it out into a website, you generally wouldn’t directly do an API call like this.
The Website would load from a database or other memory. Rather than making an API call each time.
So this script could be used as a cronjob to populate a database.

And your webpage that generates the iFrames would load from that database.

Howevery you could modifiy this script to output HTML instead. So just do that.

Thanks, solved my question! With cronjob is it than even splitted yet.

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