Get the fewest Viewer count of an game & stream?

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

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

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

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

so can not understand this?

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

so where in the code above to set the sum?

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

so thanks!

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;

so set sum somewhere here?

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

this maybe?

 $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
1 Like

$clientID is declared somewhere else!

Thanks was very helpful!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.