Php output continuous stream

痴心易碎 提交于 2019-12-06 11:06:14

问题


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:


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.



来源:https://stackoverflow.com/questions/18621770/php-output-continuous-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!