# Get recently streamed games

**URL:** <https://discuss.dev.twitch.com/t/get-recently-streamed-games/20314>\
**Category:** API\
**Created:** [March 2, 2019, 1:04am UTC](https://discuss.dev.twitch.com/t/get-recently-streamed-games/20314 "2019-03-02T01:04:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blood\_Wolf89](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/blood_wolf89/32/6786_2.png) [@Blood\_Wolf89](https://discuss.dev.twitch.com/u/Blood_Wolf89)\
**Post date:** [March 2, 2019, 1:04am UTC](https://discuss.dev.twitch.com/t/get-recently-streamed-games/20314/1 "2019-03-02T01:04:42Z")

</div>

I was looking for a way to get a list of the 5 most recently played games for a stream but could not find anything.

Here is what I did. 😊

I made a script that pulls data from _ **channels/videos** _ of the 30 most recent streams.

Now that we have a list of 30 videos we need to do something with it.

First: We get the data from the 30 most recent streams.  
Second: We make an array of the games.  
Third: We sort the games in the array from most streamed to least streamed.

Here is a **DEMO** of the script in action: [Basement-developers.com](http://basement-developers.com/projects/xiceman/games.php)  
The game data is brought in using a different api.

```
<?php
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://api.twitch.tv/kraken/channels/{CHANNEL_ID]/videos?limit=30');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Client-ID: [CLIENT_ID]",
    "Accept: application/vnd.twitchtv.v5+json",
    "Authorization: OAuth [ACESS_TOKEN]"
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // ONLY USE FOR DEVELOPMENT WITHOUT SSL

$result = curl_exec($ch);

curl_close($ch);

$data = json_decode($result, true);

$games = array(); // Creates empty array for games
foreach($data['videos'] as $item) {
    if ($item['game'] != ''){ // Checks if video has game name. If not it is ignored.
        $games[] = $item['game'];
    }
}

$gc = array_count_values($games); // This will is counts the number of times game name is displayed.

arsort($gc); // This will sort the array of from most to least.

var_dump($gc); // Here you can see the array. Should look something like this: array(5) { ["Atlas"]=> int(10) ["Tom Clancy's Rainbow Six: Siege"]=> int(8) ["Arma 3"]=> int(7) ["Minecraft"]=> int(3) ["Grand Theft Auto V"]=> int(2) }

$i = 1;
foreach($gc as $key => $value) {
    echo 'Game: '.$key.' - '.$key; // This will output a list of games with the [GameName] - [Number]

    if ($i++ == 10) break; // Limits the number of games that will be displayed.
}

```

You can edit the limit in the URL to how many videos you pull data from.  
You can edit number of games displayed by changing $i++ == 10 to what ever you want.

NOTE: The list will only give you the games that are listed in the video data. If you only played 1 game for the past 30 streams, You will only get that game in return.

Have any questions or need any help just ask. All input is welcome.

> How helpful was my post?
> 
> _Poll ([view on site](https://discuss.dev.twitch.com/t/get-recently-streamed-games/20314/1))_

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [April 1, 2019, 1:04am UTC](https://discuss.dev.twitch.com/t/get-recently-streamed-games/20314/2 "2019-04-01T01:04:47Z")

</div>

Closed on April 1, 2019, 1:04am UTC

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