如何从远程url下载mp3文件?

时间:2016-06-16 10:11:46

标签: php

我正在将我的网站重定向到只有一个mp3文件正在流式传输的网址,我希望将该文件下载到本地计算机。 我怎么能这样做?

我已经搜索了google和stacksoverflow,但解决方案对我没用 我无法找到解决我具体问题的方法。

3 个答案:

答案 0 :(得分:5)

使用CURL下载,然后你可以使用file_get_contents在服务器上保存文件,或者你可以使用一些标题来强制下载文件。

$ch = curl_init('http://url-to-file.com/audio.mp3');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
    file_put_contents(dirname(__FILE__) . '/audio.mp3', $output);
}

下载到浏览器:

$ch = curl_init('http://url-to-file.com/audio.mp3');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
    header("Content-type: application/octet-stream"); 
    header("Content-Disposition: attachment; filename=audio.mp3"); 
    echo $output;
    die();
}

答案 1 :(得分:0)

curl只下载小尺寸。

尝试更大尺寸,您可以通过下面的代码下载mp3,mp4,zip file_put_contents(dirname( FILE )。' /recordings/audio.mp3' ;, fopen(' http://url-to-file.com/audio.mp3',' r&# 39))

答案 2 :(得分:0)

有两种方法可以做到这一点。 第一种方法是使用函数readfile-它将比其他所有方法更快。

function curl_get_file_size( $url ) {
    $result = -1;    
    $curl = curl_init( $url );    
    curl_setopt( $curl, CURLOPT_NOBODY, true );curl_setopt( $curl, CURLOPT_HEADER, true );curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
    $data = curl_exec( $curl );
    curl_close( $curl );
    if( $data ) {
        $content_length = "unknown";
        $status = "unknown";
        if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) )  $status = (int)$matches[1];
        if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) )   $content_length = (int)$matches[1];
        if( $status == 200 || ($status > 300 && $status <= 308) ) 
            $result = $content_length;
    }
    return $result;
}

function run() {
        global $url,$title;
        header("Content-type: audio/mpeg");
        header("Content-Disposition: attachment; filename=$title.mp3");
        header("Content-length: " . curl_get_file_size($url));
        header("accept-ranges: bytes");
        readfile($url);
    }
run();

第二种方法是下载完整文件然后返回它

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($status == 200) {
    header("Content-type: audio/mpeg");
    header("Content-Disposition: attachment; filename=$title.mp3");
    header("Content-length: " . strlen($output));
    header("accept-ranges: bytes");
    echo $output;
    die();
}