0

いくつかの Twitch.tv ストリームを一覧表示し、それらがオフラインかオンラインかを示すスクリプトがあります。ページの 1 つに統合しようとしていますが、未定義のオフセット エラーがいくつか発生します。

これはスクリプトです:

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

$members = array("painuser","ZombieGrub","Nathanias","Youbetterknowme","ncl_tv");

// This variable becomes one long url with the channel names stringed up behind it
// This url then fetches a json file from twitch with all the selected channels information
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$userGrab .= implode(",", $members);


//I use this array to compare with the members array. All users in this arrat are substracted from the members array and hence are //"offline"
$checkedOnline = array (); 

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

}
unset($value);

//grabs the channel data from twitch.tv streams
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);

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

for ($i = 0; $i < count($members); $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>';

}
}

//This funcion add's online channel names to the checked online array
function signin($person){
if($person != null){
return $person;
}
else{
return null;
}
}

?>
<hr>
<?php
//This part list all the people currently offline. Here the array with online users is compared with the total users.
//online users are then removed from the total users array.
foreach ($members as $i => $value1) {
foreach($checkedOnline as $ii => $value2){
if($value1 == $value2){
unset($members[$i]);
}
}

}
//print a nice list with people that can't currently be bothered with streaming their games 
foreach ($members as $ii => $value2) {
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>';
echo '<a href="http://www.twitch.tv/'.$value.'"> <strong>'.$value.'</strong></a>';
echo '&nbsp<img src="/images/offline.png"> <strong> Status :</strong> Offline! </br>';

}


?>
</body>
</html>

この後、次のコードを使用してストリームを表示しています。

//This part list all the people currently offline. Here the array with online users is compared with the total users.
//online users are then removed from the total users array.
foreach ($members as $i => $value1) {
foreach($checkedOnline as $ii => $value2){
if($value1 == $value2){
unset($members[$i]);
}
}

}
//print a nice list with people that can't currently be bothered with streaming their games 
foreach ($members as $i => $value) {
echo '<a href="http://www.twitch.tv/'.$value.'"> <strong>'.$value.'</strong></a>';
echo '&nbsp<img src="/images/offline.png"> <strong> Status :</strong> Offline! </br>';
}

さて、エラー。私はこれを得ています:

Notice: Undefined offset: 0 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 42

Notice: Undefined offset: 0 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 45

Notice: Undefined offset: 1 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 42

Notice: Undefined offset: 1 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 45

Notice: Undefined offset: 2 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 42

Notice: Undefined offset: 2 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 45

Notice: Undefined offset: 3 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 42

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wp-content\themes\fesport\sidebar.php on line 75

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wp-content\themes\fesport\sidebar.php on line 84

誰か助けてくれませんか?これを解決する方法はありますか?PHPが苦手で、どうすればいいのかわかりません。

編集:2番目のエラー...

zombiegrub   Status: Online! 
Viewers:  588
nathanias   Status: Online! 
Viewers:  178
youbetterknowme   Status: Online! 
Viewers:  80
painuser   Status: Online! 
Viewers:  47

Notice: Undefined offset: 4 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 34

Notice: Undefined offset: 4 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 37

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83
Status: Online! 

Notice: Undefined variable: viewers in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 85
Viewers:  

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86
Status : Offline! 

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83
Status: Online! 

Notice: Undefined variable: viewers in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 85
Viewers:  

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86
Status : Offline! 

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83
Status: Online! 

Notice: Undefined variable: viewers in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 85
Viewers:  

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86
Status : Offline! 

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83
Status: Online! 

Notice: Undefined variable: viewers in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 85
Viewers:  

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86
Status : Offline! 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wp-content\themes\sahifa\sidebar.php on line 75

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wp-content\themes\sahifa\sidebar.php on line 84
4

1 に答える 1

-1

これは正しくありません:

 $json_array['$i']....

$iPHP は、一重引用符内の文字列を文字列リテラルとして扱います。この場合、値ではなく ,の配列値を探しています。文字列内の値を補間したい場合は、二重引用符を使用する必要があります。

 $json_array["$i"]....

ただし、変数の値を使用しているだけなので、引用符はまったく必要ありません。

 $json_array[$i]....

編集:

これも試すことができます。コンマで区切られた値の配列を渡すことになっている場合、コードは最初に余分なコンマを与えます:

$members = array("maegis","androide456","avonmexicola","iksf");

$usergrab .= implode(",", $members);

2 番目を編集します。

返される JSON は配列です。それを繰り返すには、単純なループが必要です。

for ($i = 0; $i < count($members); $i++) {
     $title = $json_array[$i]['channel']['channel_url'];
     ....
于 2013-09-14T14:59:28.593 に答える