以下评论您正在寻找的解决方案是:
<?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);
请求.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 http://icecast.omroep.nl/radio4-bb-mp3并使用该方法返回它们header()
然后你可以做fpassthru()
.
HTTP 使用新行将标头与正文分开:https://www.rfc-editor.org/rfc/rfc2616#section-6 https://www.rfc-editor.org/rfc/rfc2616#section-6
[header]
CRLF
[body]
所以应该很容易逐行解析并调用header()
until CRLF
(空行)被发现然后触发fpassthru()
.