所以我已经阅读了所有关于TwitchTV API的内容,搜索了所有的网络以获得答案,尝试了多个代码,我仍然无法使其工作。
正在发生的事情是当$ channel变量中的流不在线时,页面总是返回错误
注意:未定义的偏移量:0英寸 第13行/home/user/public_html/livestreamstatus.php
。我需要知道如何摆脱这个并只显示“离线”这个词
以下是代码:
<?php
$channel = "gamespot";
$json_file = @file_get_contents("http://api.justin.tv/api/stream/list.json?channel={$channel}", 0, null, null);
$json_array = json_decode($json_file, true);
if ($json_array[0]['name'] == "live_user_{$channel}") {
$channelTitle = $json_array[0]['channel']['title'];
$title = $json_array[0]['channel']['status'];
?>
ONLINE
<br />
<?php
} else {
?>
OFFLINE
<?php
}
?>
答案 0 :(得分:1)
你想要的是检查它是否存在,所以在打电话之前请php检查一下。
<?php
$channel = "gamespot";
$json_file = @file_get_contents("http://api.justin.tv/api/stream/list.json?channel={$channel}", 0, null, null);
$json_array = json_decode($json_file, true);
if( array_key_exists( '0', $json_array )
&& array_key_exists( 'channel', $json_array[0] )
&& $json_array[0]['name'] == "live_user_{$channel}" )
{
$channelTitle = $json_array[0]['channel']['title'];
$title = $json_array[0]['channel']['status'];
printf('Online');
}
else
{
printf('Offline');
}
?>