# Twitch stream updates

**URL:** <https://discuss.dev.twitch.com/t/twitch-stream-updates/1033>\
**Category:** API\
**Created:** [August 8, 2014, 2:25pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033 "2014-08-08T14:25:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![eguerreiro1992](https://avatars.discourse-cdn.com/v4/letter/e/22d042/32.png) [@eguerreiro1992](https://discuss.dev.twitch.com/u/eguerreiro1992)\
**Post date:** [August 8, 2014, 2:25pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/1 "2014-08-08T14:25:47Z")

</div>

Hello.  
Previously i had a php file that updated streams on my website. Since [justin.tv](http://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

---

<div class="post-metadata">

**Author:** ![night](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/night/32/1613_2.png) [@night](https://discuss.dev.twitch.com/u/night)\
**Post date:** [August 8, 2014, 7:16pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/2 "2014-08-08T19:16:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eguerreiro1992](https://avatars.discourse-cdn.com/v4/letter/e/22d042/32.png) [@eguerreiro1992](https://discuss.dev.twitch.com/u/eguerreiro1992)\
**Post date:** [August 8, 2014, 9:13pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/3 "2014-08-08T21:13:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fugiman](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/fugiman/32/108_2.png) [@Fugiman](https://discuss.dev.twitch.com/u/Fugiman)\
**Post date:** [August 8, 2014, 9:20pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/4 "2014-08-08T21:20:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eguerreiro1992](https://avatars.discourse-cdn.com/v4/letter/e/22d042/32.png) [@eguerreiro1992](https://discuss.dev.twitch.com/u/eguerreiro1992)\
**Post date:** [August 8, 2014, 9:36pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/5 "2014-08-08T21:36:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fugiman](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/fugiman/32/108_2.png) [@Fugiman](https://discuss.dev.twitch.com/u/Fugiman)\
**Post date:** [August 8, 2014, 10:38pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/6 "2014-08-08T22:38:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eguerreiro1992](https://avatars.discourse-cdn.com/v4/letter/e/22d042/32.png) [@eguerreiro1992](https://discuss.dev.twitch.com/u/eguerreiro1992)\
**Post date:** [August 8, 2014, 10:52pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/7 "2014-08-08T22:52:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![r11040c](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@r11040c](https://discuss.dev.twitch.com/u/r11040c)\
**Post date:** [October 29, 2014, 2:25pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/8 "2014-10-29T14:25:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Fugiman](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/fugiman/32/108_2.png) [@Fugiman](https://discuss.dev.twitch.com/u/Fugiman)\
**Post date:** [October 30, 2014, 12:23am UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/9 "2014-10-30T00:23:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![r11040c](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@r11040c](https://discuss.dev.twitch.com/u/r11040c)\
**Post date:** [November 2, 2014, 7:24pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/10 "2014-11-02T19:24:39Z")

</div>

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>';
```

---

<div class="post-metadata">

**Author:** ![r11040c](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@r11040c](https://discuss.dev.twitch.com/u/r11040c)\
**Post date:** [November 2, 2014, 7:25pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/11 "2014-11-02T19:25:30Z")

</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;
}
?>
```

---

<div class="post-metadata">

**Author:** ![Fugiman](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/fugiman/32/108_2.png) [@Fugiman](https://discuss.dev.twitch.com/u/Fugiman)\
**Post date:** [November 3, 2014, 11:33pm UTC](https://discuss.dev.twitch.com/t/twitch-stream-updates/1033/12 "2014-11-03T23:33:25Z")

</div>

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