我需要通过这个php文件从另一台服务器流式传输媒体文件。
<?php
$out = array(
'http'=>array(
'method'=>"GET",
'header'=>"Content-type: audio/mpegrn",
)
);
$stream = stream_context_create($out);
$end = fopen('http://example.com/audio.mp3', 'r', false, $stream);
fpassthru($end);
readfile($end);
?>
但是头不起作用。我该怎么解决这个问题?
您将标头发送到错误的方向。您所做的是通知源服务器,您将在GET请求中向其发送一些audio/mpeg
,这无论如何都是无效的,GET请求没有内容。您实际需要做的是将其发送给将接收内容的客户端。
您不应该需要流上下文来完成此任务-请尝试以下代码:
<?php
// Try and open the remote stream
if (!$stream = fopen('http://example.com/audio.mp3', 'r')) {
// If opening failed, inform the client we have no content
header('HTTP/1.1 500 Internal Server Error');
exit('Unable to open remote stream');
}
// It's probably an idea to remove the execution time limit - on Windows hosts
// this could result in the audio stream cutting off mid-flow
set_time_limit(0);
// Inform the client we will be sending it some MPEG audio
header('Content-Type: audio/mpeg');
// Send the data
fpassthru($stream);
打开后,添加
header('content-type: audio/mpeg');
or
header('content-type: application/octet-stream');