使用 CURL 从 URL 下载文件

10

我尝试使用一个php脚本从类似以下URL的地方下载文件:

http://www.xcontest.org/track.php?t=2avxjsv1.igc

我使用的代码如下,但它只生成空的下载文件:
$DLFile= "testfile.igc";
$DLURL="http://www.xcontest.org/track.php?t=2avxjsv1.igc"; 
$fp = fopen ($DLFile, 'w+');
$ch = curl_init($DLURL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);

另一个奇怪的事情是,当在网络浏览器中输入URL时,我无法获取该文件。只有在网站上点击链接时才能下载该文件!非常感谢您提供的任何建议!

所以你的代码完成了与在浏览器中打开URL相同的结果。对我来说,听起来你的代码没问题,而你正在询问如何规避反盗链保护。 - lanzz
3
我闻到了新鲜出炉的饼干的香味 :) - Ja͢ck
我在他们的网站上没有看到有关直接下载的政策,但是他们正在防止热链接。 - doublesharp
我在考虑防止热链接。但是我成功地使用下载管理器下载了这样一个文件。 - user1789813
@user1789813,如果他们同意您直接下载,请联系他们,他们应该在条款中发布该内容;否则绕过其安全措施可能是非法的,尤其是在美国。请了解DMCA - doublesharp
3个回答

29

试一试吧

<?php

    $output_filename = "testfile.igc";

    $host = "http://www.xcontest.org/track.php?t=2avxjsv1.igc";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, false);
    curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
    curl_close($ch);

    print_r($result); // prints the contents of the collected file before writing..


    // the following lines write the contents to a file in the same directory (provided permissions etc)
    $fp = fopen($output_filename, 'w');
    fwrite($fp, $result);
    fclose($fp);
?>

如果您想将其放在循环中以解析多个链接...您需要一些函数..这里是一个大致的想法...

<?php

    function collect_file($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);
        return($result);
    }

    function write_to_file($text,$new_filename){
        $fp = fopen($new_filename, 'w');
        fwrite($fp, $text);
        fclose($fp);
    }


    // start loop here

    $new_file_name = "testfile.igc";
    $url = "http://www.xcontest.org/track.php?t=2avxjsv1.igc";

    $temp_file_contents = collect_file($url);
    write_to_file($temp_file_contents,$new_file_name)

    // end loop here
?>

我刚刚在底部添加的函数可以帮助您循环并创建多个文件.. :) - Chris
2
curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org"); 这才是真正解决它的方法。 - Chris
非常好用! - chocolata
1
@TheBumpaster 您所使用的URL可能会重定向到另一个页面。将CURLOPT_FOLLOWLOCATION设置为true即可解决此问题。 - Muntashir Akon
1
使用内置的file_put_contents而不是自定义的write_to_file函数。 - Maris B.
经过多个小时的尝试后,这个解决方案完美地解决了从另一个设置了allow_url_fopen=0指令的服务器上传.zip文件的问题。在远程服务器上,可以通过链接下载.zip文件,我将文件写入本地服务器,如下所示:$my_file_uploaded = 'name.zip' $path = '/MY_UPLOAD_TEMP/'.$my_file_uploaded.'.zip'; file_put_contents( $path, $result ); - SNS - Web et Informatique

3

@Chris的答案可以用,但是这个方法似乎更好地下载非常大的文件而不会因为将整个文件下载到变量中再写入磁盘而耗尽内存:

$file_url = 'http://www.test.com/images/avatar.png';
$destination_path = "downloads/avatar.png";

$fp = fopen($destination_path, "w+");

$ch = curl_init($file_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
$st_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);

if($st_code == 200)
 echo 'File downloaded successfully!';
else
 echo 'Error downloading file!';

来源:https://www.kodingmadesimple.com/2018/02/php-download-file-from-url-curl.html

这篇文章介绍了如何使用PHP的CURL库从URL下载文件。通过设置CURL选项,可以轻松地将文件保存到本地计算机。此外,该文章还提供了一些示例代码,帮助读者快速上手并开始下载文件。

0

我在使用Curl时遇到了一些问题,无法显示(文件下载对话框),直到我做了这个:

// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $url); 
// Other Curl options ....
$output = curl_exec($ch);
if (isset($_POST["downloadExcelFile"])){ 
        // in my code the "downloadExcelFile" field
        // is sent when i'm trying to download an excel file
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="file.xls"');
        header('Cache-Control: max-age=0');
        $fp = fopen("php://output", 'w');
        fwrite($fp, $output );
        fclose($fp);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接