0

我正在使用此脚本列出一些 Twitch.tv 流及其状态(离线或在线)。

如果没有找到在线流,我希望它显示一条文字,说明所有内容都处于离线状态。

检查添加的流是否在线的代码:

//get's member names from stream url's and checks for online members
$channels = array();

for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}

unset($value);
unset($i);

//checks if player streams are online
function onlinecheck($online, $viewers)
{  
//If the variable online is not equal to null, there is a good change this person is currently streaming
if ($online != null)
{
echo '<a href="http://www.twitch.tv/'.$online.'"> <strong>'.$online.'</strong></a>';
echo '&nbsp <img src="/images/online.png"><strong> Status:</strong> Online! </br>'; 
echo '<img src="/images/viewers.png"><strong>Viewers:</strong> &nbsp' .$viewers.'</br>';

}
}

完整代码:

<html>
<head>
<title>Streamlist</title>
</head>
<body>
<?php

$members = array("ncl_tv");

$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";

$checkedOnline = array (); 

foreach($members as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}

unset($value);

$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);

$channels = array();

for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}

unset($value);
unset($i);

function onlinecheck($online, $viewers) {  
if ($online != null) {
echo '<a href="http://www.twitch.tv/'.$online.'"> <strong>'.$online.'</strong></a>';
echo '&nbsp <img src="/images/online.png"><strong> Status:</strong> Online! </br>'; 
echo '<img src="/images/viewers.png"><strong>Viewers:</strong> &nbsp' .$viewers.'</br>';        
}
}

$alloffline = "All female user streams are currently offline.";

function signin($person){
if($person != null){
return $person;
}

?>

</body>
</html>

..................................................... ..................................................... ..................................................... ……………………………………………………………………………………………………………………

4

1 回答 1

1

是因为您的$userGrabURL 包含两次用户名吗?这是您要检索其内容的 URL:

http://api.justin.tv/api/stream/list.json?channel=painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv,painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv

查看响应后,它看起来不像是导致问题的原因。$userGrab奇怪的 URL 是您在第一个循环中附加到字符串的结果foreach,之前您已经使用implode()函数调用添加了它们。我认为 twitch.tv 正确地忽略了重复的频道。

如果里面的所有值$checkedOnline都是空的,那么每个人都处于离线状态。将其放在第一个代码示例的末尾:

$personOnline = false;

foreach($checkedOnline as $person) {
    if($person !== null) {
        $personOnline = true;
        break;
    }
}

if(!$personOnline) {
    echo 'No one is online';
}
else {
    //there is at least someone online
}
于 2013-09-14T16:51:53.690 回答