使用PHP将stdout上的MP3流式传输到Jplayer [英] Streaming an MP3 on stdout to Jplayer using PHP

查看:200
本文介绍了使用PHP将stdout上的MP3流式传输到Jplayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下参数初始化jplayer:

  $ jplayer.jPlayer('setMedia',{
mp3:data.audioMP3,
oga:data.audioOGA
});

假设 data.autdioMP3 (它是例如:'http://myserver.local/playaudio.php?songID = 99& format = mp3'



我在挣扎的地方是 playaudio.php 。我想读取MP3文件并将其流式传输到jplayer,而不会透露音频的路径(这就是为什么我不用jplayer初始化音频文件的路径)。



类似(部分来自 php文件中的readfile 示例):

 <?php 

$ if($ validUser&& file_exists($ file){
header('Content-Transfer-Encoding:binary');
header('Content-Type:audio / mpeg');
header('Expires:0');
header('Cache-Control:must-revalidate');
header('Content-Length:'。filesize($ file));
ob_clean();
flush();
readfile($ file);
exit;
}
?>

我认为这只是强制文件的下载,尽管...这是否仍然显示文件到用户的路径?有没有更好的选择,将原始mp3数据流传输给用户,而不会泄露您知道的文件路径?

谢谢!

解决方案

感谢大家的支持,我的答案就是这样。



实际上,我最终解决了问题,却没有向用户透露音频文件的路径。

由于这是针对在线语音消息应用程序的,因此保护消息路径以保护用户隐私非常重要。

由于邮件存储在远程服务器上,这种情况最终变得更加复杂......发布此问题后发现这一点。



因此,所有过程都是:1)从远程服务器卷曲MP3文件 p>

2)设置一些头文件来强制下载数据,然后将它们回显到stdout中

因为数据正在被标准化并且标题被设置为强制下载,对于jPlayer的效果与它在/some/dir/message.mp3处请求文件相同,除了请求去往/ some / dir /playmessage.php - 因此路径永远不会显示给用户。

编辑** - 假设我有访问控制,验证在下面的snippit之前运行,否则将路径暴露给下面的脚本没有什么不同mp3的路径。 (请参阅我与劳埃德之间的评论)



以下是最终完成工作的代码:

  $ ch = curl_init($ remoteFile); 
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_HEADER,true);
$ data = curl_exec($ ch);
curl_close($ ch);
if($ data === false){
echo'CURL Failed';
出口;

$ b $ //获取文件大小
if(preg_match('/ Content-Length:(\d +)/',$ data,$ matches)){
$ contentLength =(int)$ matches [1];
}

//强制用户下载文件内容
header('Content-Transfer-Encoding:binary');
header('Content-Type:audio / mpeg');
header('Expires:0');
header('Cache-Control:must-revalidate');
header('Content-Length:'。$ contentLength);
ob_clean();
flush();
echo $ data;
出口;
?>

假设 $ remoteFile 是我想在服务器上播放我们要存储邮件的mp3文件。



我在使用PHP读取远程文件特别有用。以下是修改http标题的更多信息。



注意我实质上是下载了两次文件,这会降低用户的处理速度。可能有更好的方法来做到这一点。 :)如果你在本地保存文件,我最初在我的问题中的实际工作原理(使用readfile()输出原始的mp3数据)。

I'm initializing jplayer with the following parameters:

$jplayer.jPlayer('setMedia',{
  mp3: data.audioMP3,
  oga: data.audioOGA
});

Assume that data.autdioMP3 (and it's OGA counterpart) are paths to a php script, for example: 'http://myserver.local/playaudio.php?songID=99&format=mp3'

Where I am struggling is with playaudio.php. I would like to read the MP3 file and stream it to jplayer without revealing the path to the audio (this is why I am not initializing jplayer with a path to the audio file).

Something like (taken partially from the example for readfile at php docs):

<?php

$if ($validUser && file_exists($file){
     header('Content-Transfer-Encoding: binary');
     header('Content-Type: audio/mpeg');
     header('Expires: 0');
     header('Cache-Control: must-revalidate');
     header('Content-Length: ' . filesize($file));
     ob_clean();
     flush();
     readfile($file);
     exit;
}
?>

I think this just forces the download of the file though... will this still reveal the path to the file to the user? Is there a better option to stream raw mp3 data to the user without revealing the path to the file that you know of?

Thanks!

解决方案

Thanks for everyone's answers.

I actually ended up solving the problem without revealing the path to the audio file to the user.

Because this is for an online voice messaging application, it was important to protect the path to the messages in order to protect the user's privacy.

The situation ended up being further complicated by the fact that the messages are stored on a remote server... found this out right after I posted this question.

So all in all the process was:

1) Curl the MP3 file from the remote server

2) Set some headers to force the download of the data, which you then echo to stdout

Because the data is being put on standard out and the headers are set to force a download, the effect for jPlayer is the same as if it had requested a file at /some/dir/message.mp3, except that the request goes to /some/dir/playmessage.php - thus the path is never revealed to the user.

EDIT** - Assume that I have access control, validation running before the snippit below, otherwise exposing the path to the script below is no different than just exposing the path to the mp3. (see comments between me and Lloyd)

Here's the code that ended up getting the job done:

$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'CURL Failed';
  exit;
}

//Get file size
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

//force user to download file contents
header('Content-Transfer-Encoding: binary');
header('Content-Type: audio/mpeg');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . $contentLength);
ob_clean();
flush();
echo $data;
exit;
?>

Assume that $remoteFile is a path to the mp3 file that I want to play on the server where we are storing messages.

I found this resource on reading a remote file using PHP especially helpful. Here's some more information on modifying http headers.

NOTE I am essentially downloading the file twice, which slows the process down for the user. There is probably a better way to do this. :) If you kept the file locally, what I had initially in my question actually works (using readfile() to output the raw mp3 data).

这篇关于使用PHP将stdout上的MP3流式传输到Jplayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆