php输出连续流 [英] Php output continuous stream

查看:90
本文介绍了php输出连续流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在尝试PHP的fOpen函数,并且在读取声音文件方面取得了很多成功.现在,我正在尝试为广播流创建一种中继".

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可能是最差的方法,因为数据是连续的.

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

使用php套接字会产生更好的结果吗?

Would using php sockets yield better results?

Tl; dr输出连续音频/mpeg数据流的最佳方法是什么?

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

推荐答案

过去,我是通过PHP和SHOUTcast流完成此操作的.当然有可能,但是请记住,所有PHP实例都将与SHOUTcast服务器建立单独的连接.如果您使用任何PHP虚拟主机,则通常以无法运行几分钟以上的PHP脚本的方式配置这些服务器.为此,您需要自己的VPS服务器(或合作托管提供商).

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).

您是正确的,因为您将无法使用file_get_contents().根据您使用的cURL版本,您可能会因此而无法连接到SHOUTcast服务器. http://php.net/manual/zh/book.curl.php 问题通常是SHOUTcast服务器在响应中的状态行是ICY 200 OK而不是HTTP/1.0 200 OK,某些HTTP客户端会遇到麻烦.如果cURL确实适合您,请使用带有回调函数的CURLOPT_WRITEFUNCTION进行检出,以在传入数据时将数据回显给客户端.

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.

如果cURL不起作用,则需要使用fsockopen() http://www.php.net/manual/zh/function.fsockopen.php 连接到服务器并自行处理HTTP响应.

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.

这篇关于php输出连续流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆