现场音频流套接字卡在浏览器中

时间:2018-03-16 13:42:34

标签: php fsockopen internet-radio

我试图设置一个可以收听多个(私有)流的页面。 不幸的是,我无法让它运行起来。我已经尝试了Using php to opening live audio stream on android,但出于某种原因,浏览器在加载脚本时卡住了。

请参阅下面的脚本,其中包含工作主机的示例(请参阅http://icecast.omroep.nl/radio4-bb-mp3

有人可以赐教。

提前Tnx!

    $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);

1 个答案:

答案 0 :(得分:1)

以下评论您正在寻找的解决方案是:

<?php

$host = "icecast.omroep.nl";
$sub = "/radio4-bb-mp3";
header("Location:  http://{$host}{$sub}");

现在我将解释您的代码出了什么问题

标题有问题。您正在添加自己的标题和远程标题作为正文的一部分。

icecast.omroep.nl标题

HTTP/1.0 200 OK
Content-Type: audio/mpeg
Date: Sat, 24 Mar 2018 16:01:23 GMT
icy-br:192
ice-audio-info: samplerate=48000;channels=2;bitrate=192
icy-br:192
icy-genre:Classical
icy-metadata:1
icy-name:NPO Radio4
icy-pub:0
icy-url:http://www.radio4.nl
Server: Icecast 2.4.0-kh8
Cache-Control: no-cache, no-store
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, Accept, X-Requested-With, Content-Type
Access-Control-Allow-Methods: GET, OPTIONS, HEAD
Connection: Close
Expires: Mon, 26 Jul 1997 05:00:00 GMT
icy-metaint:16000

给出你的脚本index.php

<?php

$host = "icecast.omroep.nl";
$port = 80;
$sub = "/radio4-bb-mp3";


$sock = fsockopen($host,$port, $errno, $errstr, 30);
if (!$sock){
    throw new Exception("$errstr ($errno)");
}

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

<强> request.txt

GET /
[Blank line]

提供剧本

$ php -S 0.0.0.0:8000 index.php

您的脚本回复:

$ (nc 127.0.0.1 8000 < request.txt) | head -n 27

HTTP/0.9 200 OK
Date: Sat, 24 Mar 2018 16:01:23 +0000
Connection: close
X-Powered-By: PHP/7.1.14
Content-type: text/html; charset=UTF-8

HTTP/1.0 200 OK
Content-Type: audio/mpeg
Date: Sat, 24 Mar 2018 16:01:23 GMT
icy-br:192
ice-audio-info: samplerate=48000;channels=2;bitrate=192
icy-br:192
icy-genre:Classical
icy-metadata:1
icy-name:NPO Radio4
icy-pub:0
icy-url:http://www.radio4.nl
Server: Icecast 2.4.0-kh8
Cache-Control: no-cache, no-store
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, Accept, X-Requested-With, Content-Type
Access-Control-Allow-Methods: GET, OPTIONS, HEAD
Connection: Close
Expires: Mon, 26 Jul 1997 05:00:00 GMT
icy-metaint:16000

PHP正在添加自己的标题。

您需要处理从http://icecast.omroep.nl/radio4-bb-mp3收到的标头并使用header()方法返回它们,然后您可以执行fpassthru()

HTTP使用新行将标题与正文分开:https://tools.ietf.org/html/rfc2616#section-6

[header]
CRLF
[body]

因此,应该很容易逐行解析并调用header()直到找到CRLF(空行),然后触发fpassthru()