VOD ID Output? So have implemented this

  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