Get the fewest Viewer count of an game & stream?


#22

So but how is it to sum the viewer_count with json?


#23

That’s a “how do I do basic math in programming language of choice” question, not a API question


#24

ah okay thanks, but can you tell even, maybe?


#25
  1. Fetch page 0
  2. Iterate each record
  3. viewers = viewers + this records viewers
  4. fetch next page
  5. goto step 2

#26

so can not understand this?


#27

so i am very new in json yet! :slight_smile:


#28

so where in the code above to set the sum?


#29

JSON has no relevant here.

I can’t give you the code you need to use because I don’t know what language you are working in, and then if I just give you the answer you won’t have learned anything. This is basic data manipulation in any language.

You code above doesn’t have enough code to answer that


#30

so thanks!


#31

so thats all about the request:

 $channelsApi2 = 'https://api.twitch.tv/helix/streams?first=100&language=de&game_id=';
 $ch2 = curl_init();
 $apiVers2 = '&api_version=5';
 
 curl_setopt_array($ch2, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi2 . $gameID . $apiVers2
 ));

 $response2 = curl_exec($ch2);

$resp2 = json_decode($response2);
$viewerCount2 = $resp2->data[0]->viewer_count;

#32

so set sum somewhere here?


#33

$viewerCount2 = array_sum($resp2->data[0]->viewer_count);

this maybe?


#34
 $count = 0;

 $channelsApi2 = 'https://api.twitch.tv/helix/streams?first=100&language=de&game_id=';
 $ch2 = curl_init();
 
 curl_setopt_array($ch2, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi2 . $gameID
 ));

 $response2 = curl_exec($ch2);

 $resp2 = json_decode($response2);
 foreach ($resp2->data as $strim) {
   $count += $strim->viewer_count;
 }

 // get next page

#35

$clientID is declared somewhere else!


#36

Thanks was very helpful!