How to connect to a shoutcast server and listen using PHP :D

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

Post Reply
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

How to connect to a shoutcast server and listen using PHP :D

Post by Cruzado_Mainfrm »

i didn't know where to put this, it is a script to connect to a shoutcast server and listen, you can use it by adding the location of the php file to Winamp(recommended) or whatever player you use, you'll have to wait for a while before it starts listening... for those of you who wonder what the hell is this, it makes the shoutcast servers more secure because they are not letting dumbie people know where the server is, of course, the user that is going to listen has to have a fast connection because transactions between two servers are not going to be slim

Code: Select all

<?php
$sc_host="192.168.0.1";
$sc_port=8000;

//infinite time limit
set_time_limit(0);

//keeping going even if user aborts(using the STOP button for example)
ignore_user_abort();

//register the function 'byebye' to be called when the app is closed
register_shutdown_function("byebye");

function byebye() {
  $shutdown_flag=1;
  @fclose($sp);
}  

//open connection
$sp = @fsockopen("racmpc", 8000, $errno, $errstr, 30);
if (!$sp) exit("Could not connect to SHOUTcast server.\n");

set_socket_blocking($sp,false);

//Send valid headers
fwrite($sp,"GET / HTTP/1.0\nUser-Agent:SHOUTcast PHP Proxy 0.1\nicy-metadata:1\n\n");

//A little chitchatting
for ($i=0; $i<120; $i++) {
  if (feof($sp)) break;
  $str.=fread($sp,4096);
  usleep(200000);
  if (strpos($str,"\r\n\r\n")) break;
}

$head=substr($str,0,strpos($str,"\r\n\r\n"));
$head=eregi_replace("ICY 200 OK\r\n","",$head);
header($head);
flush(); //toilet?
echo substr($str,strpos($str,"\r\n\r\n")+4);
flush();

//Echo the received data
while(!$shutdown_flag) {
  $buf=fread($sp,4096);
  if (feof($sp)) $shutdown_flag=1;
  echo $buf;
  flush();
  usleep(75000);
}
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Very neat. =)

I will try this at work, when I get there on Monday. They first banned .pls and other winamp supported files, but I got past that by only using server:host adresses. Now they are blocking those addresses as well (they must be waching my logs on the proxy) so one after one they are falling away...

But this... Muahahahaha!

Speed wont be an issue using a 155mbit connection either I guess. ;)
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

the code to get the information about the radio station is this:

Code: Select all

<?php
$fp = @fsockopen("192.168.0.1", 8000, $errno, $errstr, 30);
if($fp) {
 fputs($fp,"GET /7.html HTTP/1.0\r\nUser-Agent: XML Reader(Mozilla Compatible)\r\n\r\n");
 while(!feof($fp)) {
  $dataset .= fgets($fp, 1000);
 }
fclose($fp);
 $dataset1 = $dataset;
 $headerinfo = ereg_replace("<body>.*","",$dataset);
 $dataset = ereg_replace(".*<body>", "", $dataset);
 $dataset = ereg_replace("</body>.*", ",", $dataset);
 $entries = explode(",",$dataset);
 unset($entries[7]);
 $listener=$entries[0];
 $radiostatus=$entries[1]; //1 online, 0 offline
 $uniquelisteners=$entries[2];
 $maxlisteners=$entries[3];
 $totallisteners=$entries[4];
 $bitrate=$entries[5];
 $songtitle=$entries[6];

}
print_r($entries);
?>
User avatar
Slippy
Forum Contributor
Posts: 113
Joined: Sat Jul 12, 2003 11:31 pm
Location: Vancouver eh!

Post by Slippy »

Using PHP for proxy tunnelling... it never ceases to amaze me... at least we can get our tunes ;~p

Nice script.
Post Reply