I'm trying to setup a page where several (private) streams can be listened from. Unfortunately I'm not able to get it running. I tried Using php to opening live audio stream on android already, but for some reason the browser get stuck when loading the script.
See below script with an example of a working host (see http://icecast.omroep.nl/radio4-bb-mp3)
Could someone please enlighten me.
Tnx in advance!
$host = "icecast.omroep.nl";
$port = 80;
$sub = "/radio4-bb-mp3";
$sock = fsockopen($host,$port, $errno, $errstr, 30);
if (!$sock){
throw new Exception("$errstr ($errno)");
}
header("Content-type: audio/mpeg");
header("Connection: close");
fputs($sock, "GET $sub HTTP/1\r\n");
fputs($sock, "Host: $host \r\n");
fputs($sock, "Accept: */*\r\n");
fputs($sock, "Icy-MetaData:1\r\n");
fputs($sock, "Connection: close\r\n\r\n");
fpassthru($sock);
fclose($sock);
Following comments the solution you are looking is:
Now I will explain what was wrong with your code
You have a problem with the headers. You are adding your own headers and the remote headers as part of the body.
icecast.omroep.nl headers
Given your script index.php
request.txt
Serving your script
Your script response:
PHP is adding his own headers.
You need to process the headers received from http://icecast.omroep.nl/radio4-bb-mp3 and return them using the method
header()
and then you can do thefpassthru()
.HTTP separate the headers from the body with a new line: https://tools.ietf.org/html/rfc2616#section-6
So it should be easy to parse line by line and calling
header()
untilCRLF
(empty line) is found and then trigger thefpassthru()
.