Twitch API: Trying To Return No of Viewers [PHP]


I am using PHP to return a json object, everything seems to work fine, such as returning the No of Followers. However, when trying to return the number of viewers it’s not allowing me to do this.

See Code Below:

$channels = array('ChannelName') ;
$callAPI = implode(",",$channels);
$dataArray = json_decode(@file_get_contents('' . $callAPI), true);

foreach($dataArray['streams'] as $mydata){
    if($mydata['_id'] != null){
        $name      = $mydata['channel']['display_name'];
        $viewers =   $mydata['channel']['viewers'];

If i substitute the “ChannelName” for a channel which is live, it will not set the variable $viewers.

Can you see something obvious i am doing wrong.

Viewers is nested directly under streams and not under channel. The correct line should be $viewers = $mydata['viewers'];.

Ty the URL is very hard to see the data.

Is there something i can add to make it clearer?

There are some good extensions for browsers that makes JSON easier to look through.

For Chrome:

For Firefox: