PHP shoutcast XML writer code HELP please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

PHP shoutcast XML writer code HELP please

Post by cybergirl »

Jcart | Please use

Code: Select all

and

Code: 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">&nbsp; </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"]); ?>&current=<?php print($databits["CURRENTLISTENERS"]); ?>"> 
</div> 
<table width="100%" cellspacing="0" cellpadding="0" border="0"> 
<tr> 
<td width="30" height="30" bgcolor="#CC0000">&nbsp;</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">&nbsp;</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

and

Code: 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]
Last edited by cybergirl on Wed Dec 07, 2005 3:00 pm, edited 1 time in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

please edit your post and put you code in

Code: Select all

code tags
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

Hi,

I'm not sure what you mean by

Code: Select all

code tags can you give me a quick example? 

  

I was using regular <?php ?php> tages I guess.

As soon as I see your sample I will proceed.

Thanks!

-Deborah
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

viewtopic.php?t=21171

please read the entire thread where it shows you how to use

Code: Select all

tags when posting code on these forums. you will see how beautiful it looks once you get it right.
devzer0
Forum Newbie
Posts: 1
Joined: Wed Dec 07, 2005 1:51 pm

:-)

Post by devzer0 »

means you should do this [ php ] put_ur_code_here [ / php ] (take the spaces out) will do something like below

Code: Select all

print "Hello World";
also dont use real server info and auth info in the codes u past people can take advantage, someone could DoS your mySQL server :-) even if the user is locked to a specific host , which is because i already tried to login :-)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Elaborate on "Doesn't work anymore".. What have you changed? Errors?

This is simply too much code to read through, not to mention mixed in with html which doesn't make things better :?
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

Doesn't work anymore meaning, when it is loading, it doesn't give any error messages or anything, it just takes forever, and then gives a timed out message.

It never used to do that.

I'm sure i used the right info, that the server gave me.

If there is something in there that needs to be upsdated, I'd do that, but I'm just not at this level of php programming yet.

So if you can enlighten me in any way, I might feel encouraged to learn more!

Thanks!
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

I just fixed this exact problem for rogue-radio.com - upshot of it was his router/isp was blocking the port that was being used to connect to, so we got him onto a different port - you probably are experiencing a server timeout - notice the timeout on the fsockopen is 500 seconds, whereas a php script by default times out at 30 seconds ...

so if your php script times out first, fsockopen will never return the error - try setting teh 500 in the fsock open to 29 instead and see if it gives a timeout error. if it does, I bet you are having problems with your PC's responding to teh request (maybe your firewall?)
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

trukfixer:

I tried what you said, and now the page loads, after a long wait, however, it has no info on it, not even the HTML. This seems like a step closer though. What could this means?
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

echo out the variables taht come back from the fsockopen . I suspect you have error reporting turned off, or writes to a log file only (check your error logs) but I am betting you will see a connection timeout , saying that teh connection could not be established

very likely it is a routing setup issue on your LAN , or your ISP has suddenly decided to start blocking the port which you are using..

with this guy I helped (he hosts with me, which is why I helped) , we set him up to use port 80 which we found to be open, and got it working from that point (he had to set his machine into a DMZ zone on his router)

There's a lot of steps you need to go through and check at every point- and truthfully, I was only able to help him out because I have SSH access (actually r00t) on the server, and was able to use command line stuff like telnet to see if the port was open, etc..

I dont know how I would have solved the issue if I didnt have that access, you might try http://canyouseeme.org and enter the port number you are using, to see if your network is allowing the connection in..
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

I'm not exactly what you mean by echoing out things, however, I'm seeing what you mean here. they are taking away people's abilities to do all these fund things, because it enhances their security as a server. I get it.

So if I could actually find a port to squeel through great, but I'll never figure tht out I'm sure!

Thanks I'll try and figure out what you mean, unless you can give me an example.
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

I meant FUN not "fund" things. whatever. :)
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

~snip~

Code: Select all

$fp = fsockopen ("$shout_server", $shout_port, $errno, $errstr, 500);
in that part of your code where you see that ..
immediately after that , do this:

Code: Select all

echo "<pre>";
var_dump($fp);
echo "<br>Serv:$shout_server <br>Port: $shout_port<br>Error: $errno<br>String:$errstr<br>";
and see what output you can get from it..
what that did for me was show me what the fsockopen was calling for, and the error string and number being returned
I used that to determine what to use for telnet : telnet 1.2.3.4 1221 (telnet IP port number)
which showed me that it was blocked, so I directed him to canyouseeme.org and had him enter various port numbers until we found one that was open, it turned out to be port 80 (you might try 8080 ,443,143,587,543 for example) and once we found him an open port, we edited everything to hit on that port, and finally got it all working
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

Post by cybergirl »

This is the message I got:

bool(false)
Serv:142.161.111.217 Port: 80Error: 10060String:A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
cybergirl
Forum Commoner
Posts: 36
Joined: Wed Dec 07, 2005 1:11 pm

YAY!

Post by cybergirl »

I tried 8080, and it worked!

You did it! You helped me fix it.

:D I'm sooo happy! :D

Here's where it is in case you are wanted to see it:

http://neurogirl.com/shout/playlist.php
Post Reply