Php output continuous stream

1.6k views Asked by At

So I've been experimenting with PHP's fOpen function and I've had a lot of success with reading sound files. Now, I'm trying to create a sort of "relay" for shoutcast streams.

File_get_contents is probably the poorest way of doing this, since the data is continuous.

Would using php sockets yield better results?

Tl;dr What's the best way to output a continuous stream of audio/mpeg data?

1

There are 1 answers

9
Brad On

I've done this with PHP and SHOUTcast streams in the past. It's certainly possible, but keep in mind that all of your PHP instances will be making separate connections to your SHOUTcast server. If you're using any PHP web hosting, those servers are typically configured in such a way that running a PHP script for more than a few minutes is impossible. For this to work, you'll need your own server for VPS (or a cooperative hosting provider).

You are correct in that you won't be able to use file_get_contents() for this. Depending on your version of cURL, you might get away with connecting to the SHOUTcast server with it. http://php.net/manual/en/book.curl.php The problem generally is that SHOUTcast servers' status lines in their responses are ICY 200 OK instead of HTTP/1.0 200 OK, which some HTTP clients have trouble with. If cURL does work for you, check out using CURLOPT_WRITEFUNCTION with a callback function to echo data back to the client as it comes in.

If cURL doesn't work, you are going to need to use fsockopen() http://www.php.net/manual/en/function.fsockopen.php to connect to the server and handle the HTTP response yourself.

From there, all you have to do is echo the data back to the client as it comes in.