So basically I want to write a php script that will run periodically to see if a channel is live. i have the following code:
<?php
$client_id = 'lqhpxo5cq53rbp2pzmb71qjlusqsw2';//Twitch client id AKA: api key
$user = 'raythgaming';//Streamers username
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/streams?user_login=$user");//Endpoint
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Client-ID: $client_id"//Add auth
));
$profile_data = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($profile_data);
if (!isset($profile_data['data'][0])) {
$live = 0;//Not live
} else {
$live = 1;//Is live
}
if ($live == 1) {
$title = $profile_data['data'][0]['title'];
$viewer_count = $profile_data['data'][0]['viewer_count'];
$game_id = $profile_data['data'][0]['game_id'];
$went_live_at = DateTime::createFromFormat("Y-m-d H:i:s", date("Y-m-d H:i:s", strtotime($profile_data['data'][0]['started_at'])))->format('Y-m-d H:i:s');
$started = date_create($went_live_at);
$now = date_create(date('Y-m-d H:i:s'));
$diff = date_diff($started, $now);
$hours = $diff->h;
$minutes = $diff->i;
echo "<br/>$user is playing $game_name, started streaming $hours hours $minutes minutes ago and has $viewer_count viewers TITLE: $title";
} else {
echo "<br/>$user is not live";
}
When I run this I’m getting:
Array ( [error] => Unauthorized [status] => 401 [message] => OAuth token is missing )
raythgaming is not live
I’ve tried looking through the docs and cannot for the life of me work out how to get an OAuth token to add into the headers which I know how to do fine.