0

I'm trying to request more than 100 live channels from the Twitch API. The current limit of results are 100. I need to do a new request every 100 while adding +1 to the offset url. I've tried to search to find help but I haven't been able to find anything.

<?php
$get2 = json_decode 
(@file_get_contents_curl('https://api.twitch.tv/kraken/streams/? 
offset=0&limit=5'), true);


foreach ($get2['streams'] as $test) {
    echo "<pre>";
    print_r ($test['channel']['name'] . ' | ' . $test['viewers']);
    echo "</pre>";
}
?>

This will echo 100 results perfectly. I just need to loop that code x however many times I need to get to the total. So say there are 30,000 live streams. That will be 300 queries.

Brayden
  • 26
  • 3
  • How about putting the whole of the code in a for loop which defines the offset (so something like `for ($offset=0; $offset<100; $offset+=5)` – Nigel Ren Jul 19 '19 at 13:02
  • Some reading material https://discuss.dev.twitch.tv/t/is-there-a-limit-to-the-amount-of-channels-i-can-search-for-with-one-request/17043/3 – Andreas Jul 19 '19 at 13:03

1 Answers1

0

Figured out what I had to do after staying up all night working on it. This will take the request and loop it for as many times as I need to get the full results.

for ($offset = 0; $offset < $requests; $offset++) {
    $testz = 
    json_decode(@file_get_contents_curl('https://api.twitch.tv/kraken/streams/? 
    offset=' . $offset . '&limit=100'), true);
    foreach ($testz['streams'] as $streamz) {
        echo "<pre>";
        print_r ($streamz['channel']['name']);
        echo "</pre>";
    }
}
Brayden
  • 26
  • 3