使用cUrl下载音频文件,获取文件所需的身份验证

时间:2014-11-13 15:40:53

标签: php curl

  

我想从服务器下载音频文件。

     

当我直接访问服务器上的url时,会询问用户名&密码我想通过cURL覆盖它

     

基本上我想允许我的网络用户在我的网站上收听歌曲。我有其他网站的身份验证。目前我网站的用户首先使用凭据进行双重检查,然后他们点击“播放”按钮然后再次输入用户名&我提供的密码(来自我的同一个网站有身份验证)。

$url="https://example.com/abc.wav";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "userstring:passString");
curl_exec($ch);
curl_close($ch);

Direct URL after entering the username & password allowed me to listen the audio.
now how can I authenticate my web to access that URL by overriding authentication?
I want to download it and as well as play in the background after clicking on button "download" & "Play" respectively.
On the other hand is there any way that I can email that file without downloading or by downloading it

1 个答案:

答案 0 :(得分:1)

  

使用以下代码进行身份验证,忘记设置标题

header("Content-Type: audio/mpeg");
$url = "clip.mp3"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "password");
curl_exec($ch);
header('Location: ' . $url);