Hi, how i can do total sum viewers if i have for example 3 or 4 channels using “https://api.twitch.tv/helix/streams?user_login=channel1&user_login=channel4&user_login=channel4&user_login=channel4 ” ?
example:
channel1 = 5
channel2 = 7
channel3 = 3
channel4 = 8
TOTAL VIEWERS = 23
Parse the JSON response and add it up yourself in your code
Well you already wrote code to go and fetch the response so you just use the language you are working in JSON parser to tell it to parse the response to JSON then iterate over the results.
What code do you currently have?
This for print and use API:
$cho = curl_init('https://id.twitch.tv/oauth2/token');
curl_setopt($cho, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cho, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cho, CURLOPT_POST, 1);
$fields = array(
'client_id' => 'c533u8ntdp29h42xxzwfe0pf5v66dm',
'client_secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'grant_type' => 'client_credentials'
);
curl_setopt($cho, CURLOPT_POSTFIELDS, $fields);
$output = curl_exec($cho);
$info = curl_getinfo($cho);
// print_r($info);
$oauth = json_decode($output, true);
$token = $oauth['access_token'];
// print_r($token);
// inizio a chiamare le API
$all_channels = [];
foreach($channels as $chans){
if (count($chans) > 0) {
$callAPI = implode('&user_login=', $chans);
$url="https://api.twitch.tv/helix/streams?user_login=" . $callAPI;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Client-ID: c533u8ntdp29h42xxzwfe0pf5v66dm',
'Authorization: Bearer '.$token
));
$result = curl_exec($ch);
$i = curl_getinfo($ch);
curl_close($ch);
// preparo le stamp per le API
$str = json_decode($result, true);
if ($i['http_code'] == 200) {
// play print API
foreach ($str['data'] as $live_chan){
$all_channels[] = $live_chan;
}
// do stuff with $str has chnanels
} else {
// non 200 do somethign with the error
// do stuff with $str has error message
}
// print_r($url);
// fine chiamata API
}
}
This for count total views (but not works):
foreach($all_channels as $mydata){
$views = $mydata['viewer_count'];
$viewss = (array)$views;
$viewsss = implode('+', (array)$views);
$totv = array_sum($viewsss);
}
echo $totv;
Errr yeah no idea what you are trying to do with that bit of PHP there…
if ($i['http_code'] == 200) {
// play print API
$total = 0;
foreach ($str['data'] as $live_chan){
$total += $live_chan['viewer_count'];
}
echo $total;
} else {
// non 200 do somethign with the error
// do stuff with $str has error message
}
the code translate me json_decode on array all_channels[]
I don’t understand your statement
with $all_channels[] i can print all_channels[‘data’][‘viewer_count’] from principal array ( [data] => [viewer_count], [user_id], etc…)
so in foreach i use $all_channels as $mydata for simplify on $mydata[‘viewer_count’]
i know, it’s dirty but i don’t know how clean it now
My code fix removed the need for all_channels
if you don’t want to use that fix then
foreach($all_channels as $mydata){
$views = $mydata['viewer_count'];
$viewss = (array)$views;
$viewsss = implode('+', (array)$views);
$totv = array_sum($viewsss);
}
echo $totv;
instead do
$total = 0;
foreach($all_channels as $mydata){
$total += $mydata['viewer_count'];
}
echo $total;
Theres no need to do the weird array, implode and array_sum stuff you are doing
i love you.
so, how i can remove the need for all_channels? i don’t know your code fix
Something like
<?php
$cho = curl_init('https://id.twitch.tv/oauth2/token');
curl_setopt($cho, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cho, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cho, CURLOPT_POST, 1);
$fields = array(
'client_id' => 'c533u8ntdp29h42xxzwfe0pf5v66dm',
'client_secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'grant_type' => 'client_credentials'
);
curl_setopt($cho, CURLOPT_POSTFIELDS, $fields);
$output = curl_exec($cho);
$info = curl_getinfo($cho);
if ($info['http_code'] == 200) {
$oauth = json_decode($output, true);
$token = $oauth['access_token'];
// inizio a chiamare le API
$total = 0;
foreach($channels as $chans){
if (count($chans) > 0) {
$callAPI = implode('&user_login=', $chans);
$url="https://api.twitch.tv/helix/streams?user_login=" . $callAPI;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Client-ID: c533u8ntdp29h42xxzwfe0pf5v66dm',
'Authorization: Bearer '.$token
));
$result = curl_exec($ch);
$i = curl_getinfo($ch);
curl_close($ch);
// preparo le stamp per le API
if ($i['http_code'] == 200) {
$str = json_decode($result, true);
foreach ($str['data'] as $live_chan){
$total += $live_chan['viewer_count'];
}
// do stuff with $str has chnanels
} else {
// non 200 do somethign with the error
// do stuff with $str has error message
}
// fine chiamata API
}
echo $total;
}
But most of this is a full “I wouldn’t of done it like this”, but works as a rough example.
You don’t want to generate an app access token every time you should reuse one you already have if it’s still valid
okay thanks, i love u 3000
1 Like
system
Closed
June 15, 2020, 6:43pm
15
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.