PHP,列出两次东西

时间:2013-09-14 16:38:16

标签: php foreach arguments

我正在使用此脚本列出一些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>

............................................... .................................................. .................................................. .........................

1 个答案:

答案 0 :(得分:1)

是因为您的$userGrab网址包含两次用户名吗?这是您正在检索其内容的网址:

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

看了一下响应后,它看起来不像是导致问题。奇怪的网址是您在之前使用$userGrab函数调用添加它们之后,在第一个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
}