如何在PHP文档返回的八位字节流上强制使用标头


How do I force a header on an octet-stream returned by a PHP document?

我已经试着在这个网站上找到这个问题的答案,但我有点没有经验,我的知识也有限。

我的情况如下:我有一个外部PHP文件(比如http://example.org/get.php)(我无法编辑),它将MP3文件作为八位位组流返回。问题是,我需要它是一个音频/mp3,以便与HTML5音频播放器一起使用。我怎样才能做到这一点?

我想我不能只做这样的事情

$mp3_url = 'http://example.org/get.php?file=123'; header('Content-Type: audio/mp3');

,可以吗?

一种方法是在服务器上设置代理,例如/your/script.php可能看起来像

header('Content-Type: audio/mp3', true, 200);
echo file_get_contents($_GET['url']);

(跳过安全检查)

然后您可以将其用作<a href="/your/script.php?url=http%3A%2F%2Fexample.org%2Fget.php%3Ffile%3D123">link</a>

另一种方法是使用Web服务器(如Nginx或Apache)设置代理。

使用另一个php脚本作为代理,即:

<?php
$mp3 = file_get_contents("http://example.org/get.php?file=123");
header('Content-Type: audio/mp3');
echo $mp3;

使用audio/mpeg作为内容类型