Search code examples
phptwitch

Can't display Twitch API data using PHP


I want to display the last 100 followers on Twitch using their API on my website. However, I know very little about JSON.

Currently I have this:

$string = file_get_contents("https://api.twitch.tv/kraken/channels/pewdiepie/follows.json?limit=100");
$json=json_decode($string,true);

But I can't get it in a while loop.

I tried this:

foreach ($json as $key => $value){
echo  $value['name'];
}

Can someone help me? I just want the last 100 follower names displayed on the page.


Solution

  • Assuming that getting the data works, this should do it:

    foreach ($json['follows'] as $follow){
        echo  $follow['user']['name'];
    }