Twitch stream updates

Hello.
Previously i had a php file that updated streams on my website. Since justin.tv closed it stop working. I already tried to put the new api so that it works again, but as i read somewhere that twitch kraken is not compatible with xml. So can anyone give me some tips on what to do? Here is the 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 = 'http://api.justin.tv/api/stream/list.xml?channel='.$channel;
		    $twitch_xml = simplexml_load_file($twitch_api);
		    $viewers = $twitch_xml->stream->channel_count;
		    $thumb = $twitch_xml->stream->channel->screen_cap_url_large;
		    if($twitch_xml->stream->stream_type == 'live') $live = 1;
		    else $live = 0;
		break;
		default:
		break;
		
		}
	
	$query = "UPDATE ".PREFIX."streams SET status='".$live."', viewers='".$viewers."', thumb='".$thumb."' WHERE streamID='".$streamID."'";
	$result=$mysqli->query($query);
}
$mysqli->close;
?>

Hope you can help me solve this issue

PHP supports JSON. Check the PHP docs at http://php.net/manual/en/book.json.php

Hello
What that php file does is gets the column channel from the website database that includes the name of the channels. Then i run a cron task on the cpanel running this php file. So i’m a little stuck on this issue. Is this code right or am i missing something

$twitch_api = 'https://api.twitch.tv/kraken/streams/list.json?channel='.$channel;
$twitch_xml = json_decode($twitch_api);

I really want to get this working again fast because many people see streams trow my website

$twitch_api = 'https://api.twitch.tv/kraken/streams/'.$channel;
$twitch_xml = json_decode(@file_get_contents($twitch_api));

Hello Fugiman thanks for your reply. The problem persists. The code i have at the moment is:

<?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_xml = json_decode(@file_get_contents($twitch_api));
		    $viewers = $twitch_xml->stream->channel_count;
		    $thumb = $twitch_xml->stream->channel->screen_cap_url_large;
		    if($twitch_xml->stream->stream_type == 'live') $live = 1;
		    else $live = 0;
		break;
                default:
		break;
		
		}
	
	$query = "UPDATE ".PREFIX."streams SET status='".$live."', viewers='".$viewers."', thumb='".$thumb."' WHERE streamID='".$streamID."'";
	$result=$mysqli->query($query);
}
$mysqli->close;
?>

Don’t know whats wrong hope you can help me

My hope was that you’d use my snippet of code to learn about the differences yourself and fix the rest of your code appropriately…

<?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();
?>

Hello Fujiman. Thanks for the help. I was trying to solve this issue but i’m not so good at coding. I will try to understand what you have send me and comment all the lines for future updates. Thanks for your fast help.
Best Regards

I to would like to know if this snippet of code is correct. I have the same setup and have a cron job pointing to an"update_steams.php" file with this code inserted. my streams stopped on my web page and i am assuming something in here has been updated.

Nothing should have changed recently. If you would like to share your code, perhaps I could debug it further? Do you have any logs from the cronjob that may explain what is going wrong?

This actually worked. I had entered my crontab incorrect. I am able to locate steams now on my site with the above code but when i click on the preview box of my site it says “The requested URL /stream// was not found on this server.”

where streams.php is located there is this code that calls to it

echo '<div class="sm_gallery" style="background:url('.$thumb.') #12181C; background-size: 249px 142px; width:216px;">
       <div class="gallery_overlay"><img src="images/games/'.$game.'.gif" alt="" />
    <small>'.$viewers.' VIEWERS</small><br/>
	<a href="stream/'.$streamID.'/">'.$title.'</a>
    </div>
</div>';

here is the entire 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) $chatz = '<div style="float:left;"><iframe width="650" height="400" frameborder="0" scrolling="no" id="chat_embed" src="http://twitch.tv/chat/embed?channel='.$chat.'&amp;popout_chat=true"></iframe></div>';
				
		// Advertising
		$allbanner = safe_query("SELECT * FROM ".PREFIX."bannerrotation WHERE displayed='1' AND bannertype='1' ORDER BY RAND() LIMIT 0,1");
		$total = mysql_num_rows($allbanner);
		if($total) {
			$banner = mysql_fetch_array($allbanner);
			$pub = '<div style="width:650px; height:80px; margin:10px 0px 10px 10px; float:left;"><a href="ads/out/'.$banner['bannerID'].'/" target="_blank"><img style="width:650px;" src="./images/bannerrotation/'.$banner['banner'].'" border="0" alt="'.htmlspecialchars($banner['bannername']).'" /></a></div>';
		}
		else $pub = '';
		
		
		eval("\$title_stream = \"".gettemplate("stream_header")."\";");
		echo $title_stream;
		 
		if($type == 1) echo '<object type="application/x-shockwave-flash" height="360" width="670" id="live_embed_player_flash" data="http://en.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://en.twitch.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="hostname=en.twitch.tv&channel='.$channel.'&auto_play=true&start_volume=25" /></object>'.$pub.'';
		elseif($type == 2) echo '<iframe frameborder="0" scrolling="no" src="http://www.own3d.tv/liveembed/'.$channel.'?autoPlay=true" height="360" width="670"></iframe>'.$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;
		
		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 = '<img src="images/flags/'.$country['short'].'.gif" width="18" height="12" style="margin: 3px 5px 0px 0px; float:left;" />';
		
			if($type == 1) $type_img = '<img src="gfx/streams/twitch.png" class="type" />';
			elseif($type == 2) $type_img = '<img src="gfx/streams/own3d.png" class="type" />';
		
			echo '<div class="sm_gallery" style="background:url('.$thumb.') #12181C; background-size: 249px 142px; width:216px;">
    <div class="gallery_overlay"><img src="images/games/'.$game.'.gif" alt="" />
    <small>'.$viewers.' VIEWERS</small><br/>
	<a href="stream/'.$streamID.'/">'.$title.'</a>
    </div>
</div>';
				
		}
		
		eval("\$title_streams = \"".gettemplate("streams_featured_bottom")."\";");
		echo $title_streams;
		
	}
	
	
	eval("\$title_streams = \"".gettemplate("streams_header")."\";");
	echo $title_streams;
	$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 = '<img src="images/flags/'.$country['short'].'.gif" width="18" height="12" style="margin: 3px 5px 0px 0px; float:left;" />';
		
			if($type == 1) $type_img = '<img src="gfx/streams/twitch.png" class="type" />';
			elseif($type == 2) $type_img = '<img src="gfx/streams/own3d.png" class="type" />';
		
			echo '<div class="sm_gallery" style="background:url('.$thumb.') #12181C; background-size: 249px 142px; width:216px;">
       <div class="gallery_overlay"><img src="images/games/'.$game.'.gif" alt="" />
    <small>'.$viewers.' VIEWERS</small><br/>
	<a href="stream/'.$streamID.'/">'.$title.'</a>
    </div>
</div>';
				
		}
	
	} else echo '<div style="margin: 7px 0px 0px 0px;float: left;">No Streams Available.</div>';
	eval("\$title_streams = \"".gettemplate("streams_bottom")."\";");
	echo $title_streams;
}
?>

I’m not sure what’s going wrong, but it seems to be an issue of $streamID being improperly set?