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

**URL:** <https://discuss.dev.twitch.com/t/twitch-api-trying-to-return-no-of-viewers-php/1075>\
**Category:** API\
**Created:** [September 9, 2014, 7:18am UTC](https://discuss.dev.twitch.com/t/twitch-api-trying-to-return-no-of-viewers-php/1075 "2014-09-09T07:18:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![khank](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@khank](https://discuss.dev.twitch.com/u/khank)\
**Post date:** [September 9, 2014, 7:18am UTC](https://discuss.dev.twitch.com/t/twitch-api-trying-to-return-no-of-viewers-php/1075/1 "2014-09-09T07:18:01Z")

</div>

Hi,

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('https://api.twitch.tv/kraken/streams?channel=' . $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.

---

<div class="post-metadata">

**Author:** ![george](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/george/32/120_2.png) [@george](https://discuss.dev.twitch.com/u/george)\
**Post date:** [September 9, 2014, 9:18am UTC](https://discuss.dev.twitch.com/t/twitch-api-trying-to-return-no-of-viewers-php/1075/2 "2014-09-09T09:18:15Z")

</div>

> [@khank](#):
>
> $viewers = $mydata[‘channel’][‘viewers’];

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

---

<div class="post-metadata">

**Author:** ![khank](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@khank](https://discuss.dev.twitch.com/u/khank)\
**Post date:** [September 9, 2014, 9:59am UTC](https://discuss.dev.twitch.com/t/twitch-api-trying-to-return-no-of-viewers-php/1075/3 "2014-09-09T09:59:04Z")

</div>

> [@khank](#):
>
> [https://api.twitch.tv/kraken/streams?channel](https://api.twitch.tv/kraken/streams?channel)

Ty the URL is very hard to see the data.

[https://api.twitch.tv/kraken/streams?channel](https://api.twitch.tv/kraken/streams?channel)

Is there something i can add to make it clearer?

---

<div class="post-metadata">

**Author:** ![Sunspots](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/sunspots/32/1835_2.png) [@Sunspots](https://discuss.dev.twitch.com/u/Sunspots)\
**Post date:** [September 9, 2014, 2:55pm UTC](https://discuss.dev.twitch.com/t/twitch-api-trying-to-return-no-of-viewers-php/1075/4 "2014-09-09T14:55:53Z")

</div>

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

For Chrome:

- [JSONview](https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc)
- [JSON Viewer](https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh/reviews)
- etc.

For Firefox:

- [JSONview](https://addons.mozilla.org/en-US/firefox/addon/jsonview/?src=search)

- [JSONovich](https://addons.mozilla.org/en-US/firefox/addon/jsonovich/?src=search)

- etc.
