Search Streams [V5] can I get multiple queries in one call?

Hi,

I’m currently using the kraken/search/streams endpoint to find all streams with the term ‘3v3’ in the title for a particular game. This works fine, however, I also want to search a few other terms and want to avoid making separate calls for each search.

This is the current way that works for a single term search

search/streams?limit=100&offset=$index&query=3v3

I was wondering if there is something available for multi term searches like this

search/streams?limit=100&offset=$index&query=3v3&query=treeline

Here’s the relevant code for reference. I’d have to duplicate all of it for each search term and that’s hard on my resources and makes for extra api calls.

$data = [];
$headers = [];

// fetch all streams with '3v3'
$index = 0;
$max = 0;
do {
    // handle ratelimiting
    if (array_key_exists('Ratelimit-Remaining', $headers) && $headers['Ratelimit-Remaining'] < 1) {
        sleep($headers['Ratelimit-Reset'] - time());
    }
    $headers = [];
    
    // build the url
    $url = "https://api.twitch.tv/kraken/search/streams?limit=100&offset=$index&query=3v3";
    
    // get the next chunk of streams
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Accept: application/vnd.twitchtv.v5+json",
        "Client-ID: " . $twitchClientId
    ));
    curl_setopt($ch, CURLOPT_HEADERFUNCTION,
        function($curl, $header) use (&$headers) {// this function is called by curl for each header received
            $len = strlen($header);
            $header = explode(':', $header, 2);
            if (count($header) < 2) // ignore invalid headers
                return $len;
            $name = strtolower(trim($header[0]));
            if (!array_key_exists($name, $headers))
                $headers[$name] = [trim($header[1])];
            else
                $headers[$name][] = trim($header[1]);
            return $len;
        }
    );
    $result = curl_exec($ch);
    $jsonResult = json_decode($result, true);
    curl_close($ch);
    
    // parse the results
    if (array_key_exists('_total', $jsonResult)) {
        $max = $jsonResult['_total'];
    }
    if (array_key_exists('streams', $jsonResult) && count($jsonResult['streams']) > 0) {
        foreach ($jsonResult['streams'] as $current) {
            if ($current['game'] == "League of Legends") {
                $data[] = array('display_name' => $current['channel']['display_name'],
                               'viewers' => $current['viewers'],
                               'title' => $current['channel']['status'],
                               'twitch_id' => $current['channel']['_id']);
            }
        }
    }
    
    // increment the loop
    $index += 100;
}
while ($index < $max);

Any help or advice would be helpful. I’ve found other old solutions but they were hacky and used what looked like sql vulnerabilities to get it done. I want to know if there is anything officially supported to do this.

Try this

search/streams?limit=100&offset=$index&query=3v3+treeline

Thanks for the reply.
However, I tried adding the ‘+treeline’ but it didn’t work as intended. I’m pretty sure it interprets the + as a space so it looks for ‘3v3 treeline’ instead of the desired ‘3v3’ or ‘treeline’.

Hey! Unfortunately, I don’t think this is possible with the current implementation of the endpoint. It doesn’t look like there is anything officially supported to do this. Sorry!

Ok, thanks, I’ll just have to to it the long way.

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