如何在 PHP 文档返回的 octet-stream 上强制使用 header?

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

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

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

我想我不能只做像

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

,可以吗?

使用 audio/mpeg 作为内容类型

一种方法是在您的服务器上设置代理,例如/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>

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

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

<?php

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