【问题标题】:Download file from URL using CURL使用 CURL 从 URL 下载文件
【发布时间】:2012-10-31 22:11:35
【问题描述】:

我尝试使用 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 时,我没有得到文件。只有点击web site!上的链接才能下载文件。

非常感谢任何建议!

【问题讨论】:

  • 因此您的代码实现了与在浏览器中打开 URL 相同的结果。听起来你的代码没问题,你问的是如何规避反盗链保护。
  • 我闻到新鲜出炉的饼干 :)
  • 我在他们的网站上没有看到关于直接下载的政策,但他们正在阻止热链接。
  • 我在考虑防止盗链。但我成功尝试使用下载管理器下载这样的文件。
  • @user1789813 你应该联系他们直接下载,如果他们说可以,他们应该按照他们的条款发布,否则绕过他们的安全可能是非法的,尤其是在美国。 DMCA

标签: php curl download


【解决方案1】:

试一试

<?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
?>

【讨论】:

  • 非常感谢,这是解决方案!
  • 如果你想循环创建多个文件,我刚刚在底部添加的函数会有所帮助.. :)
  • curl_setopt($ch, CURLOPT_REFERER, "xcontest.org"); 真正解决了它。
  • 像魅力一样工作!
  • @TheBumpaster 您使用的 URL 可能会重定向到另一个页面。将CURLOPT_FOLLOWLOCATION 设置为true 即可解决此问题。
【解决方案2】:

@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

【讨论】:

    【解决方案3】:

    在我这样做之前,我在使用 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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2016-05-02
      • 2017-12-24
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多