Stream Status New Twitch API Upgrade


#1

Hi Everyone,

some time ago someone gave me a PHP script for my website to display a list of online Twitch channels. Now that script is not working anymore. Unfortunately i have no clue what to change to get it working again.

It would be great if someone can help me out here and tell me what to change for the new Twitch API.

This is what i got:

    class TwitchConnector
    {
        public static function getStreamsFromTwitch($stream_list)
        {
            $mycurl = curl_init();  
            $url = "https://api.twitch.tv/kraken/streams?channel=" . $stream_list;
    		$clientId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    		curl_setopt_array($mycurl, array(
        		CURLOPT_HTTPHEADER => array('Client-ID: '. $clientId),
        		CURLOPT_RETURNTRANSFER => true,
        		CURLOPT_URL => $url));
            curl_setopt($mycurl, CURLOPT_URL, $url);
            $web_response = curl_exec($mycurl);
            $response = json_decode($web_response);

            $streams = array();
            if($response != null && !empty($response) && $response->streams != null) {
                foreach ($response->streams as $stream) {
                    $streamObj = new Stream();
                    $streamObj->setViewerCount($stream->viewers);
                    $streamObj->setPreviewImage($stream->preview->medium);
                    $streamObj->setDisplayname($stream->channel->display_name);
                    $streamObj->setGame($stream->channel->game);
                    $streamObj->setCaster($stream->channel->name);
                    $streamObj->setDescription(htmlspecialchars($stream->channel->status));
                    $streams[] = $streamObj;
                }
            }
    		return $streams;
        }
    }

There also is a 2nd PHP File, which is providing the $stream_list

Thank you very much :slightly_smiling_face:


#2

Please refer to the migration guide

The following will convert you to V5

    		curl_setopt_array($mycurl, array(
        		CURLOPT_HTTPHEADER => array('Client-ID: '. $clientId, 'Accept': 'application/vnd.twitchtv.v5+json'),
        		CURLOPT_RETURNTRANSFER => true,
        		CURLOPT_URL => $url));

And $stream_list needs to be a user_id not a user_name

Or you can rewrite the script completely to support New API: