下载带有网址变量的文件卷曲


Download file Curl with url var

我想用 Curl 下载一个文件。问题是下载链接不是直接的,例如:

http://localhost/download.php?id=13456

当我尝试使用 curl 下载文件时,它会下载文件下载.php!

这是我的卷曲代码:

        ###
        function DownloadTorrent($a) {
                    $save_to = $this->torrentfolder; // Set torrent folder for download
                    $filename = str_replace('.torrent', '.stf', basename($a));
                    $fp = fopen ($this->torrentfolder.strtolower($filename), 'w+');//This is the file where we save the information
                    $ch = curl_init($a);//Here is the file we are downloading
                    curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // Important 
                    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
                    curl_setopt($ch, CURLOPT_URL, $fp);
                    curl_setopt($ch, CURLOPT_HEADER,0); // None header
                    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // Binary trasfer 1
                    curl_exec($ch);
                    curl_close($ch);
                    fclose($fp); 
    }

有没有办法在不知道路径的情况下下载文件?

你可以试试CURLOPT_FOLLOWLOCATION

如果为 TRUE,则跟在服务器作为一部分发送的任何"位置:"标头之后 的 HTTP 标头(注意这是递归的,PHP 将遵循尽可能多的 "位置:"标头,除非CURLOPT_MAXREDIRS 集)。

因此,这将导致:

function DownloadTorrent($a) {
    $save_to = $this->torrentfolder; // Set torrent folder for download
    $filename = str_replace('.torrent', '.stf', basename($a));
    $fp = fopen ($this->torrentfolder.strtolower($filename), 'w+');//This is the file where we save the information
    $ch = curl_init($a);//Here is the file we are downloading
    curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // Important 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER,0); // None header
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // Binary transfer 1
    curl_exec($ch);
    curl_close($ch);
    fclose($fp); 
}

将 FOLLOWLOCATION 选项设置为 true,例如:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

选项记录在此处:http://www.php.net/manual/en/function.curl-setopt.php

哦!

CURLOPT_FOLLOWLOCATION工作完美...

问题是我使用 CURLOPT_URL for fopen(),我只是简单地更改CURLOPT_URL白色CURLOPT_FILE

而且效果很好!谢谢你的帮助=)