Sort play streams by Online or Offline

Hi,

Is there a way to sort player stream in a way that all the online stream would always be on top?

I’m currently using this code as twitch.php:

    <?php
    
    $stream = $_GET['stream'];
    
    $json_file = @curl("http://api.justin.tv/api/stream/list.json?channel={$stream}");
    $json_array = json_decode($json_file, true);
    
    if (strtolower($json_array[0]['name']) == strtolower("live_user_{$stream}")) {
        echo file_get_contents("online.png");
    }else{
        echo file_get_contents("offline.png");
    }

    function curl($url, $post = null, $retries = 3){
        $curl = curl_init($url);

        if(is_resource($curl) === true){
                curl_setopt($curl, CURLOPT_FAILONERROR, true);
                curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

                if(isset($post) === true){
                        curl_setopt($curl, CURLOPT_POST, true);
                        curl_setopt($curl, CURLOPT_POSTFIELDS, (is_array($post) === true) ? http_build_query($post, "", "&"): $post);
                }

                $result = false;

                while(($result === false) && (--$retries > 0)){
                        $result = curl_exec($curl);
                }

                curl_close($curl);
        }
        return $result;

}

?>

Thanks & Regards,

stream/list only returns live streams. A “stream” is by definition live. “Channels” would include both online and offline channels.

Also, if you are intending your application to only support Twitch channels, I’d recommend using the Twitch Kraken API instead of the legacy Justin.tv API.

https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streams