Php 如何修复流媒体的此标头?

Php 如何修复流媒体的此标头?,php,header,stream,Php,Header,Stream,我需要通过这个php文件从另一台服务器传输媒体文件 <?php $out = array( 'http'=>array( 'method'=>"GET", 'header'=>"Content-type: audio/mpeg\r\n", ) ); $stream = stream_context_create($out); $end = fopen('http://example.com/audio.mp3', 'r', false, $s

我需要通过这个php文件从另一台服务器传输媒体文件

<?php
$out = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Content-type: audio/mpeg\r\n", 
  )
);

$stream = stream_context_create($out);

$end = fopen('http://example.com/audio.mp3', 'r', false, $stream);
fpassthru($end);
readfile($end);
?>
但是标题不起作用。
如何解决此问题?

您发送的标题方向错误。您所做的是通知源服务器您将在GET请求中向其发送一些音频/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);
在fopen之后,添加

header('content-type: audio/mpeg');
or
header('content-type: application/octet-stream');

是否还需要内容长度?很好的解决方案!但在iOS 5.0.1中,即使使用128kbps的mp3,流媒体速度也有点慢。你知道为什么吗?PD:我是从一个更快的VPS流媒体。你是如何测试这个的?通过移动网络或WiFi?请记住,您受到客户端和远程服务器之间传输链中最慢链路的限制。因此,如果客户端的internet连接不能承受128kbps+的数据包开销,它将无法工作。同样,如果您的服务器遇到带宽问题,它也将无法工作,原始源服务器也是如此。由于您同时从源服务器下载数据并将其上载到客户端,因此服务器需要为每个并发流分配128k+的对称数据包开销。这是实时媒体流协议通常使用UDP的一个主要原因。由于无线连接固有的损耗特性,这在无线连接上不太可行,但它大大减少了开销,应该尽可能地实施。UDP流更容易因丢失的数据包无序到达而发生小错误,但TCP流可能会导致重新传输,这将增加所需的带宽和数据流继续的等待时间。第二个示例通常会强制浏览器下载。