Question

I'm trying to find the best way to pull data about users down to display on my site. In the long run, the usernames will be coming from a MYSQL database, but for now I just created a small array. For testing purposes, I would love for it to spit out (username from array) and viewer count. But I'm having no luck.

Here is my code below, the code below I was just trying to output anything from the two usernames, but it was coming up empty.

<?php
$streamstats = array();
$username = array("streamerhouse", "cohhcarnage");

foreach($username as $stream) {
$data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $stream)); 
$streamstats[] = $stream;
$print_r($streamstats);
}
?>

Which I get nothing. Any advice?

Was it helpful?

Solution

You have 2 errors. First you have $ before print_r. Second one is that you print it in a foreach loop, but you should print everything after the loop. Give a shot code below:

$streamstats = array();
$username = array("streamerhouse", "cohhcarnage");

foreach($username as $stream) {
   $data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $stream)); 
   $streamstats[] = $data;
}
print_r($streamstats);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top