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;