How to connect to a shoutcast server and listen using PHP :D
Posted: Fri Oct 03, 2003 8:47 pm
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);
}
?>