Help with Stream update - Twitch Api

Hi Guys,

I have some problems with my streams updates. On my website all the Twitch streams added by us should appear when they are online and should dissapear when they are offline. The problem is that the offline and online streams dont appear at all. Or another problem is when the offline streams are still appear instead of dissapear from streams list.

Here is my code:

<?php include("_mysql.php"); $mysqli = new mysqli($host, $user, $pwd, $db); $streams = $mysqli->query("SELECT * FROM ".PREFIX."streams"); while ($ds = $streams->fetch_assoc()) { $streamID = $ds['streamID']; $channel = $ds['channel']; $type = $ds['type']; switch($type) { case 1: // Twitch Stream API $twitch_api = 'https://api.twitch.tv/kraken/streams/'.$channel; $twitch_json = json_decode(@file_get_contents($twitch_api)); if ($twitch_json && $twitch_json->stream) { $live = 1; $viewers = $twitch_json->stream->viewers; $thumb = $twitch_json->stream->preview->large; } else { $live = 0; } break; } $query = "UPDATE ".PREFIX."streams SET status='".$live."', viewers='".$viewers."', thumb='".$thumb."' WHERE streamID='".$streamID."'"; $result = $mysqli->query($query); } $mysqli->close(); ?>

Can somebody help me with this problem ? :frowning:

Thank you very much,

You’re missing a client ID in your request. Also it is recommended to use curl instead of file get contents so you can catch http error codes that the API might return.

1 Like

I don’t see the client ID anywhere. Here is the blog outlining the requirement: https://blog.twitch.tv/client-id-required-for-kraken-api-calls-afbb8e95f843#.3z0b8thhh There are lots of threads on the past week or so showing how to add it.

1 Like

Hi 3ventic,

I put there the client ID. Still doesn’t work. How i can use curl ? I am not a programmer at all :(.

My code looks like this now:

<?php include("_mysql.php"); $mysqli = new mysqli($host, $user, $pwd, $db); $streams = $mysqli->query("SELECT * FROM ".PREFIX."streams"); while ($ds = $streams->fetch_assoc()) { $streamID = $ds['streamID']; $channel = $ds['channel']; $type = $ds['type']; switch($type) { case 1: // Twitch Stream API $twitch_api = 'https://api.twitch.tv/kraken/streams/'.$channel; // Input your Client-ID here. Obtained by registering the API through Twitch. $ClientId = ""; $ch = curl_init(); curl_setopt_array($ch, array(CURLOPT_HTTPHEADER => array('Client-ID: ' . $ClientId), CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $twitch_api)); $result = curl_exec($ch); curl_close($ch); $twitch_json = json_decode($result, true); if ($twitch_json && $twitch_json->stream) { $live = 1; $viewers = $twitch_json->stream->viewers; $thumb = $twitch_json->stream->preview->large; } else { $live = 0; } break; } $query = "UPDATE ".PREFIX."streams SET status='".$live."', viewers='".$viewers."', thumb='".$thumb."' WHERE streamID='".$streamID."'"; $result = $mysqli->query($query); } $mysqli->close(); ?>

Below you will see the value set for a client ID, a function called “file_get_contents_curl” which can replace “file_get_contents” in your scripts and an example of it being used.

This method will put your client ID in the header of the request. I believe this is the preferred method of sending your client ID.

<?php

$clientID = array(
‘Client-ID: 0000000000000000000000000000000’
);

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       
curl_setopt($ch, CURLOPT_HTTPHEADER, $clientID);
$data = curl_exec($ch);
curl_close($ch);
return $data;

}

$apiCALL = json_decode(@file_get_contents_curl(‘https://api.twitch.tv/kraken/channels/matt_thomas/follows?limit=1’), true);
$follower = $apiCALL[‘follows’][‘0’][‘user’][‘name’];
echo $follower;

?>

Thank you very much Thomas but i dont know how to implement it in my code. I dont know what i need to delete or add and where.

Thanks anyway.

<?php include("_mysql.php"); $mysqli = new mysqli($host, $user, $pwd, $db);

$clientID = array(
‘Client-ID: 0000000000000000000000000000000’
);

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $clientID);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

$streams = $mysqli->query("SELECT * FROM ".PREFIX.“streams”);
while ($ds = $streams->fetch_assoc()) {
$streamID = $ds[‘streamID’];
$channel = $ds[‘channel’];
$type = $ds[‘type’];

switch($type) {

case 1: // Twitch Stream API
    $twitch_api = 'https://api.twitch.tv/kraken/streams/'.$channel;
    $twitch_json = json_decode(@file_get_contents_curl($twitch_api));
    if ($twitch_json && $twitch_json->stream) {
        $live = 1;
        $viewers = $twitch_json->stream->viewers;
        $thumb = $twitch_json->stream->preview->large;
    } else {
        $live = 0;
    }
    break;
}

$query = “UPDATE “.PREFIX.“streams SET status='”.$live.”‘, viewers=’”.$viewers.“‘, thumb=’”.$thumb.“’ WHERE streamID='”.$streamID.“'”;
$result = $mysqli->query($query);
}
$mysqli->close();
?>

Manny thanks. It worked Thomas but the video is not loading :frowning: . Strange … . Also not all the channels are listed. Just 2 for the moment…

Url: http://acleague.ro/streams/

Where is the player div?I see content-left content-right and footer under content. I don’t see a player anywhere in it.

I have other php file. I think is here. Streams.php .

<?php if( isset( $_GET['streamID'] ) ) $streamID = $_GET['streamID']; else $streamID = ''; if($streamID) { $stream=safe_query("SELECT * FROM ".PREFIX."streams WHERE streamID='".$streamID."'"); while($ds=mysql_fetch_array($stream)) { $streamID = $ds['streamID']; $channel = $ds['channel']; $title = $ds['title']; $type = $ds['type']; $status = $ds['status']; $viewers = $ds['viewers']; $thumb = $ds['thumb']; $chat = $ds['chat']; if($type == 1) $type_banner = ''; elseif($type == 2) $type_banner = ''; if($type == 1) $chatz = '
'; elseif($type == 2) $chatz = '
'; // Advertising $allbanner = safe_query("SELECT * FROM ".PREFIX."bannerrotation WHERE displayed='1' AND bannertype='4' ORDER BY RAND() LIMIT 0,1"); $total = mysql_num_rows($allbanner); if($total) { $banner = mysql_fetch_array($allbanner); $pub = '
'.htmlspecialchars($banner['bannername']).'
'; } else $pub = ''; eval("\$title_stream = \"".gettemplate("stream_header")."\";"); echo $title_stream; if($type == 1) echo '
'.$pub.''; elseif($type == 2) echo '
'.$pub.''; eval("\$title_stream = \"".gettemplate("stream_bottom")."\";"); echo $title_stream; } } else { $featured=safe_query("SELECT * FROM ".PREFIX."streams WHERE displayed='1' AND status='1' AND featured='1' ORDER BY viewers DESC"); if(mysql_num_rows($featured)) { eval("\$title_streams = \"".gettemplate("streams_featured_header")."\";"); echo $title_streams; echo '
    '; while($ds=mysql_fetch_array($featured)) { $streamID = $ds['streamID']; $title = $ds['title']; $info = $ds['info']; $type = $ds['type']; $channel = $ds['channel']; $status = $ds['status']; $viewers = $ds['viewers']; $thumb = $ds['thumb']; $game = mysql_fetch_array(safe_query("SELECT tag FROM ".PREFIX."games WHERE gameID = '".$ds['gameID']."' LIMIT 0,1")); $game = $game['tag']; $country = mysql_fetch_array(safe_query("SELECT short FROM ".PREFIX."countries WHERE countryID = '".$ds['countryID']."' LIMIT 0,1")); $country = ''; if($type == 1) $type_img = ''; elseif($type == 2) $type_img = ''; echo '
  • '.$country.''.$title.' '.$info.' '.$viewers.' '.$type_img.'
  • '; } echo '
'; eval("\$title_streams = \"".gettemplate("streams_featured_bottom")."\";"); echo $title_streams; } eval("\$title_streams = \"".gettemplate("streams_header")."\";"); echo $title_streams; echo '
    '; $streams=safe_query("SELECT * FROM ".PREFIX."streams WHERE displayed='1' AND status='1' AND featured='0' ORDER BY viewers DESC"); if(mysql_num_rows($streams)) { while($ds=mysql_fetch_array($streams)) { $streamID = $ds['streamID']; $title = $ds['title']; $info = $ds['info']; $type = $ds['type']; $channel = $ds['channel']; $status = $ds['status']; $viewers = $ds['viewers']; $thumb = $ds['thumb']; $game = mysql_fetch_array(safe_query("SELECT tag FROM ".PREFIX."games WHERE gameID = '".$ds['gameID']."' LIMIT 0,1")); $game = $game['tag']; $country = mysql_fetch_array(safe_query("SELECT short FROM ".PREFIX."countries WHERE countryID = '".$ds['countryID']."' LIMIT 0,1")); $country = ''; if($type == 1) $type_img = ''; elseif($type == 2) $type_img = ''; echo '
  • '.$country.''.$title.' '.$info.' '.$viewers.' '.$type_img.'
  • '; } } else echo '
    No Streams Available.
    '; echo '
'; eval("\$title_streams = \"".gettemplate("streams_bottom")."\";"); echo $title_streams; } ?>

@aclgamestv, you’re loading in the old Flash player, which is deprecated. That’s why the video isn’t playing. Please check out the video embed documentation and update your code to use the new embed.

1 Like

Thanks Chains. I try to figure it out but no success.

Have you tried changing your line:

if($type == 1) echo '<div style="margin:7px 0px 0px 0px;float:left;"><object type="application/x-shockwave-flash" height="360" width="634" id="live_embed_player_flash" data="http://pt-br.twitch.tv/widgets/live_embed_player.swf?channel='.$channel.'" bgcolor="#000000"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="movie" value="http://pt-br.twitch.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="hostname=pt-br.twitch.tv&channel='.$channel.'&auto_play=true&start_volume=25" /></object></div>'.$pub.'';

to

if($type == 1) echo '<div style="margin:7px 0px 0px 0px;float:left;"><iframe src="http://player.twitch.tv/?channel='.$channel.'" height="360" width="634" frameborder="0" scrolling="no" allowfullscreen="true"></iframe>></div>'.$pub.'';

Thomas you are the man here. It worked :D.

Manny thanks :).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.