Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twitch API和PHP(查看拖缆数据)_Php_Twitch - Fatal编程技术网

Twitch API和PHP(查看拖缆数据)

Twitch API和PHP(查看拖缆数据),php,twitch,Php,Twitch,我正试图找到最好的方法,把用户的数据拉下来显示在我的网站上。从长远来看,用户名将来自MYSQL数据库,但现在我只创建了一个小数组。出于测试目的,我希望它能吐出(数组中的用户名)和查看器计数。但是我没有运气 下面是我的代码,下面的代码我只是想从这两个用户名中输出任何东西,但结果是空的 <?php $streamstats = array(); $username = array("streamerhouse", "cohhcarnage"); foreach($username as $s

我正试图找到最好的方法,把用户的数据拉下来显示在我的网站上。从长远来看,用户名将来自MYSQL数据库,但现在我只创建了一个小数组。出于测试目的,我希望它能吐出(数组中的用户名)和查看器计数。但是我没有运气

下面是我的代码,下面的代码我只是想从这两个用户名中输出任何东西,但结果是空的

<?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);
}
?>


我什么也得不到。有什么建议吗?

您有两个错误。首先,您有$before
print\r
。第二个是在foreach循环中打印它,但是应该在循环之后打印所有内容。在下面给出一个快照代码:

$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);

成功了!愚蠢的错误。你知道我现在如何从中提取各个字段吗$streamstats->title;我假设,对吗?在foreach循环中,您可以向数组中添加标题,而不是整个数据,例如,替换:
$streamstats[]=$data带有
$streamstats[]=$data[0]->标题