2

我有一个远程链接,我想从中强制下载文件。现在我正在使用 cURL。

我的脚本适用于这个网址(因为我测试了一个不同的网址):http: //images.mob.org/iphonegame_img/rip_curl_surfing_game_live_the_search/real/1_rip_curl_surfing_game_live_the_search.jpg

但我需要它来处理这个: https ://googledrive.com/host/0B_3oJnpnNoF9UjlkVUwtWE5CY0U/city.jpg

这是我的 PHP 代码

 <?php
    $file = 'http://images.mob.org/iphonegame_img/rip_curl_surfing_game_live_the_search/real/1_rip_curl_surfing_game_live_the_search.jpg';
    download($file,2000);

    function download($file,$chunks){
        set_time_limit(0);
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-disposition: attachment; filename='.basename($file));
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        header('Pragma: public');
        $size = get_size($file);
        header('Content-Length: '.$size);

        $i = 0;
        while($i<=$size){
            //Output the chunk
            get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks));
            $i = ($i+$chunks);
        }

    }

    //Callback function for CURLOPT_WRITEFUNCTION, This is what prints the chunk
    function chunk($ch, $str) {
        print($str);
        return strlen($str);
    }

    //Function to get a range of bytes from the remote file
    function get_chunk($file,$start,$end){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $file);
        curl_setopt($ch, CURLOPT_RANGE, $start.'-'.$end);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'chunk');
        $result = curl_exec($ch);
        curl_close($ch);
    }

    //Get total size of file
    function get_size($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_exec($ch);
        $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
        return intval($size);
    }

    ?>

我不知道该怎么办知道。请指教一些东西。

4

2 回答 2

21

您不需要复杂的代码来完成简单的任务,这应该可以正常工作

$file = 'https://googledrive.com/host/0B_3oJnpnNoF9UjlkVUwtWE5CY0U/city.jpg';
download($file);

function download($url) {
    set_time_limit(0);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $r = curl_exec($ch);
    curl_close($ch);
    header('Expires: 0'); // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
    header('Cache-Control: private', false);
    header('Content-Type: application/force-download');
    header('Content-Disposition: attachment; filename="' . basename($url) . '"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . strlen($r)); // provide file size
    header('Connection: close');
    echo $r;
}
于 2013-01-04T01:38:14.890 回答
0

假设您也没有 SSL 证书问题,问题是 Google似乎没有返回 HEAD 请求的内容长度。

所以删除 CURLOPT_NOBODY 行,脚本就可以工作了。

编辑:Barmar 已经在上面提到了 Google Drive 的内容长度问题,但该解决方案仍然适用。

于 2013-01-04T01:53:00.650 回答