PHP shoutcast XML writer code HELP please
Posted: Wed Dec 07, 2005 1:24 pm
Jcart | Please use
code #2 index.php
----------------------------------------
Jcart | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Help me!
I'm not familiar with php, and I was using this free snippet but it doesn't want to work for me anymore, and I don't know why because it always did work. I'll send the code below to look at, and I would like to ask if something is obsolete or if something in there has been messed up (all the server info is correct):
code #1 - config.php
-------------------------------------Code: Select all
<?php
error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE);
//SHOUTcast server settings, needed to get the XML output from the DNAS
$shout_server = "REMOVED BY JCART";
$shout_port = "8000";
$shout_password = "REMOVED BY JCART";
//MySQL settings, you can use this script to save info about the state of the server listner info and song histories, the only limitation being that it would only work
//if people if people vist the page regularly. You could get round this by putting a META refresh tag in the page and keeping it open on a PC somwhere.
$use_mysql_features = false;
$mysql_server = "REMOVED BY JCART";
$mysql_user = "REMOVED BY JCART";
$mysql_pass = "REMOVED BY JCART";
$mysql_db = "CyberOp_radio";
?>code #2 index.php
----------------------------------------
Code: Select all
<?php
require_once (realpath("config.php"));
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>SHOUTcast - Streaming Audio</title>
<link rel="stylesheet" type="text/css" href="ie.css" />
</head>
<body topmargin="0" leftmargin="0" bgcolor="#333333" text="#FF0000" link="#000000" vlink="#000000" alink="#000000">
<script language="Javascript">
function songHistory(title, time) {
newWindow = window.open("",'','scrollbars=yes, toolbars=no, width=600, height=400');
newWindow.self.location = "song_hist.php?command=today&st=" + title + "&pa=" + time;
}
</script>
<?php
$xmldata = "";
$write = "";
$databits[] = Array();
$depth = 0;
$songcount = 0;
$songs[][] = Array();
$users[][] = Array();
$usercount = 0;
$fp = fsockopen ("$shout_server", $shout_port, $errno, $errstr, 500);
if (!$fp) {
switch ($errno) {
case 9:
?>
<font face="verdana" size="5" color="#CC0000"><b>Unable to connect</b></font>
<br><br>
<font size="2" face="verdana" color="#CC0000">
This script was unable to connect to the SHOUTcast DNAS.
</font>
</body>
</html>
<?php
break;
}
exit;
} else {
fputs ($fp, "GET /admin.cgi?pass=$shout_password&mode=viewxml&page=1 HTTP/1.0\r\nHost: localhost\r\nUser-agent: Mozilla\r\n\r\n");
while (!feof($fp)) {
$xmldata .= fgets ($fp,128);
}
fclose ($fp);
$xmldata = substr($xmldata, strpos($xmldata, "<?xml"));
$xmldata = eregi_replace("'", "'", $xmldata);
function startElement($parser, $name, $attrs) {
global $write;
global $databits;
$databits["$name"] = "";
$write = "$name";
}
function endElement($parser, $name) {
global $write;
global $databits;
}
function characterData($parser, $data) {
global $write;
global $databits;
$databits["$write"] = $data;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_parse($xml_parser, $xmldata, strlen($xmldata));
xml_parser_free($xml_parser);
if ($use_mysql_features == true) {
$link = mysql_connect ("$mysql_server", "$mysql_user", "$mysql_pass");
mysql_select_db("$mysql_db", $link);
$datenow = date("Y-m-d");
$timenow = date("H:i:s");
$cl = $databits["CURRENTLISTENERS"];
$pl = $databits["PEAKLISTENERS"];
$ml = $databits["MAXLISTENERS"];
$rl = $databits["REPORTEDLISTENERS"];
$results = mysql_query("insert into listenerhistory (endate, entime, cl, pl, ml, rl) values ('$datenow', '$timenow', $cl, $pl, $ml, $rl)", $link);
mysql_close ($link);
}
$xmldata = "";
$fp = fsockopen ("$shout_server", $shout_port, $errno, $errstr, 30);
fputs ($fp, "GET /admin.cgi?pass=$shout_password&mode=viewxml&page=4 HTTP/1.0\r\nHost: localhost\r\nUser-agent: Mozilla\r\n\r\n");
while (!feof($fp)) {
$xmldata .= fgets ($fp,128);
}
fclose ($fp);
$xmldata = substr($xmldata, strpos($xmldata, "<?xml"));
$xmldata = eregi_replace("'", "'", $xmldata);
function startElementA($parser, $name, $attrs) {
global $write;
global $songcount;
switch ($name) {
case "SONG":
$songcount++;
break;
case "PLAYEDAT":
$write = "TIME";
break;
case "TITLE":
$write = "TITLE";
break;
}
}
function endElementA($parser, $name) {
global $write;
}
function characterDataA($parser, $data) {
global $write;
global $songs;
global $songcount;
$songs[$songcount]["$write"] = $data;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElementA", "endElementA");
xml_set_character_data_handler($xml_parser, "characterDataA");
xml_parse($xml_parser, $xmldata, strlen($xmldata));
xml_parser_free($xml_parser);
$xmldata = "";
$fp = fsockopen ("$shout_server", $shout_port, $errno, $errstr, 30);
fputs ($fp, "GET /admin.cgi?pass=$shout_password&mode=viewxml&page=3 HTTP/1.0\r\nHost: localhost\r\nUser-agent: Mozilla\r\n\r\n");
while (!feof($fp)) {
$xmldata .= fgets ($fp,128);
}
fclose ($fp);
$xmldata = substr($xmldata, strpos($xmldata, "<?xml"));
$xmldata = eregi_replace("'", "'", $xmldata);
function startElementB($parser, $name, $attrs) {
global $write;
global $usercount;
switch ($name) {
case "LISTENER":
$usercount++;
break;
}
$write = $name;
}
function endElementB($parser, $name) {
global $write;
}
function characterDataB($parser, $data) {
global $write;
global $users;
global $usercount;
$users[$usercount]["$write"] = $data;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElementB", "endElementB");
xml_set_character_data_handler($xml_parser, "characterDataB");
xml_parse($xml_parser, $xmldata, strlen($xmldata));
xml_parser_free($xml_parser);
$now_playing = fopen ("nowplaying.txt", "w");
fputs ($now_playing, $songs[1]["TITLE"]);
fclose ($now_playing);
?>
<span id="st" style="font-family: verdana; color: #CC0000; font-size: 30pt"><?php print($databits["SERVERTITLE"]); ?></span>
<div>
<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="70%" align="left">
<font color="#000000" size="3">
<b>Current Song: </b><font color="#CC0000"><?php print($databits["SONGTITLE"]); ?></font> - <a href="http://<?php print("$shout_server:$shout_port"); ?>/listen.pls"><font size="3"><b>LISTEN</b></font></a>
</font>
</td>
<td width="30%" align="right"> </td>
</tr></table>
</div>
<table width="100%" cellspacing="2" cellpadding="0" border="0">
<tr>
<td width="100%" colspan="2" vAlign="top">
<div class="panelheader" style="margin-bottom: 2px;">Server & Broadcaster Information</div>
<font color="#CC0000">
<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="70%">
<table width="100%" cellspacing="0" cellpadding="0" border-"0">
<tr>
<td width="20%"><b>Server Status:</b></td>
<?php
if ($databits["STREAMSTATUS"] == "1") {
$altm = round($databits["AVERAGETIME"]/60);
$alts = round($databits["AVERAGETIME"]%60);
?>
<td width="80%">Server has source connected and is available to the public</td>
</tr>
<tr>
<td width="20%"><b>Server Name:</b></td>
<td width="80%"><?php print($databits["SERVERTITLE"]) ?></td>
</tr>
<tr>
<td width="20%"><b>Average Listen Time:</b></td>
<td width="80%"><?php print("$altm minutes $alts seconds") ?></td>
</tr>
<tr>
<td width="20%"><b>Music Genre:</b></td>
<td width="80%"><?php print($databits["SERVERGENRE"]) ?></td>
</tr>
<tr>
<td width="20%"><b>Stream URL:</b></td>
<td width="80%"><a href="<?php print($databits["SERVERURL"]) ?>" target="_blank"><?php print($databits["SERVERURL"]) ?></a></td>
</tr>
<tr>
<td width="20%"><b>Currently Streaming:</b></td>
<td width="80%"><span style="width: 200px;"><marquee><i><?php print($databits["SONGTITLE"]) ?></i></marquee></span></td>
</tr>
<tr>
<td width="20%"><b>Server date & time:</b></td>
<td width="80%"><?php print(date("D j\<\s\u\p\>S\<\/\s\u\p\> M Y @ H:i:s")); ?></td>
</tr>
<?php
} else {
?>
<td width="80%">Server is down</td>
</tr>
<?php
}
?>
</table>
</td><td class="pic1" width="30%" align="right">
</td></tr></table>
</font>
</tr>
</tr>
<tr>
<td width="50%" vAlign="top">
<div class="panelheader" style="margin-bottom: 2px;">Songs & Song History</div>
<div class="songc">
<?php
if ($use_mysql_features == true) {
$link = mysql_connect ("$mysql_server", "$mysql_user", "$mysql_pass");
mysql_select_db("$mysql_db", $link);
$datenow = date("Y-m-d");
$timenow = date("H:i:s");
}
for ($i=1;$i<=$songcount;$i++) {
$st = $songs[$i]["TITLE"];
$pa = $songs[$i]["TIME"];
$st = addcslashes($st, "'");
if ($use_mysql_features == true) {
$result = mysql_query("INSERT INTO songhistory (endate, entime, songtitle, playedat) VALUES ('$datenow', '$timenow', '$st', '$pa')", $link);
}
if ($i == 1) {
?>
<div class="song" style="border-left-color: #CC0000; border-right-color: #CC0000; background-color: #FF3300"><font size="1"><b>Song Title: </b>
<?php
} else {
?>
<div class="song"><font size="1" color="#000000"><b>Song Title: </b>
<?php
}
?>
<?php
if ($use_mysql_features == true) {
print ($songs[$i]["TITLE"]);
print (" <a href=\"javascript:songHistory('$st', '$pa');\"><font size=\"1\" color=\"#CC0000\"><b>Song History</b></font></a>");
} else {
print ($songs[$i]["TITLE"]);
}
?>
<br>
<b>Played @: </b><?php print (date("h:i:s a",$songs[$i]["TIME"])); ?>
<br>
</font></div>
<br>
<?php
}
if ($use_mysql_features == true) {
mysql_close($link);
}
?>
</div>
</tr>
<td width="50%" vAlign="top">
<div class="panelheader">Server Capacity & Utilisation</div>
<table width="100%" cellspacing="2" cellpadding="0" border="0">
<tr>
<td width="50%" vAlign="top">
<div class="subpanel" style="margin-bottom: 2px;">Stream Quality</div>
This server is pumping tunes out @<br><br>
<center>
<font size="6" color="#CC0000"><?php print($databits["BITRATE"]) ?> kbp/s</font>
</center>
<br>
<?php
$linkspeed = $databits["BITRATE"]*$databits["MAXLISTENERS"];
?>
It is therefore (or should be) connected to a network link providing <?php print($linkspeed) ?> kbp/s of bandwidth in order to provide it's maximum of
<?php print ($databits["MAXLISTENERS"]); ?> with decent service.
<br><br>
<?php
if ($databits["BITRATE"] < 56) {
?>
You <b>can</b> listen to this stream using a modem class connection (or greater).
<?php
} else {
if ($databits["BITRATE"] < 128) {
?>
You <b>can</b> listen to this stream using a ISDN class connection (or greater). This stream will not work using a standard modem.
<?php
} else {
if ($databits["BITRATE"] > 128) {
?>
You <b>need</b> a broadband connection of at least <?php print($databits["BITRATE"]) ?> kbp/s downstream speed to listen to this stream!
<?php
}
}
}
?>
</td>
<td width="50%" vAlign="top">
<div class="subpanel" style="margin-bottom: 2px;">Listener Counts</div>
<div align="left"><font size="3">
<?php
print ($databits["CURRENTLISTENERS"]);
print ("/");
print ($databits["MAXLISTENERS"]);
?>
</font> <br>
<img src="listener_graphic.php?max=<?php print($databits["MAXLISTENERS"]); ?>¤t=<?php print($databits["CURRENTLISTENERS"]); ?>">
</div>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="30" height="30" bgcolor="#CC0000"> </td>
<td vAlign="middle" style="padding-left: 0.1in;"><font size="1">
This line denotes the number of listeners <b>currently</b> connected to the server.
</font>
</td>
</tr>
<tr>
<td width="30" height="30" bgcolor="#008000"> </td>
<td vAlign="middle" style="padding-left: 0.1in;"><font size="1">
This line denotes the <b>peak</b> number of users that have been connected at any one time.
</font>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<div class="panelheader">Listener Information</div>
<table width="100%" cellspacing="2" cellpadding="0" border="0">
<tr>
<td width="50%" vAlign="top">
<div class="subpanel" style="margin-bottom: 2px;">Listener Numbers</div>
There are:<br>
<div style="margin-left: 0.3in;">
<font size="5"><?php
$plural = ($databits["CURRENTLISTENERS"] == 1) ? '' : 's';
print($databits["CURRENTLISTENERS"]);
print ("</font> current listener$plural<br>");
?>
and listener levels have peaked @ <font size="5"><?php print($databits["PEAKLISTENERS"]); ?></font>.<br>
</div>
<br>
</td>
<td width="50%" vAlign="top">
<div class="subpanel" style="margin-bottom: 2px;">Aggregate Values</div>
<?php
$uas[][] = Array();
$totaltime = 0;
$totalunderruns = 0;
$uasnum = 0;
for ($i=1;$i<=$usercount;$i++) {
$totaltime = $totaltime + $users[$i]["CONNECTTIME"];
$totalunderruns = $totalunderruns + $users[$i]["UNDERRUNS"];
}
$totalmins = round(($totaltime/60));
$totalsecs = round($totaltime%60);
$disptime = "<b>$totalmins</b> minutes <b>$totalsecs</b> seconds";
$lis = $databits["CURRENTLISTENERS"];
$plural = ($lis == 1) ? '' : 's';
print ("The <b>$lis</b> connected user$plural have been connected for a total of $disptime, there have been a total of <b>$totalunderruns</b> underruns.<br><br><b>User Agents:</b>");
?>
<div class="useragent">
<?php
for ($i=1;$i<=$usercount;$i++) {
$found=false;
$ua = $users[$i]["USERAGENT"];
for ($j=0;$j<=$uasnum;$j++) {
if ($uas[$j]["UA"] == $ua) {
$found=true;
$uas[$j]["NUM"]++;
break;
}
}
if ($found == false) {
$uasnum++;
$uas[$uasnum]["UA"] = $ua;
$uas[$uasnum]["NUM"] = 1;
}
}
?>
<table width="100%" cellspacing="0" border="0" cellpadding="0">
<tr>
<td width="75%"><font size="1"><b>User Agent</b></font></td>
<td width="25%" align="right"><font size="1"><b>Users</b></font></td>
</tr>
<?php
for ($i=0;$i<=(count($uas)-1);$i++) {
$ua2 = $uas[$i]["UA"];
$num = $uas[$i]["NUM"];
print ("<tr>");
print ("<td width=\"75%\"><font size=\"1\">$ua2</font></td><td width=\"25%\" align=\"right\"><font size=\"1\">$num</font></td>");
print ("</tr>");
}
?>
</table>
</div>
</td>
</tr>
</table>
<?php
}
?>
</body>
</html>Jcart | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]