使用 cURL 下载 zip 文件不适用于以下 php 代码


Using cURL to download a zip file isn't working with follow php code

我尝试过/正在尝试的

function download_data($target_document,$outfile_location) 
{
    log_message('info', 'Downloading every page in data set');
    do 
    {   
       $ch = curl_init ($target_document.'apiKey=' . self::$API_KEY);       
       $fp = fopen($outfile_location . "data.zip", "w");
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        fclose($fp);
        $this->http_status = $http_status;
            if($this->http_status != 200) 
            {
                $this->data_retry_count += 1;   
            }
    } while($this->http_status != 200 && $this->data_retry_count < $this->retry_max);
        if($this->http_status == 200) 
        {
            return;
        }
}

使用上面的代码,一切都很顺利,除了我得到一个没有内容的压缩(zip)。如果我在浏览器中下载目标文档,它可以工作。我是否必须对 curl 做一些不同的事情才能获取 zip 文件的内容?

忽略$this变量,因为此函数是类的一部分,并且这些变量表示类中包含的变量。

这应该可以解决你的问题。

这将下载 ZIP,而不会为空,这是不久前我问题的修复程序。

此外,还应考虑echo curl_error($ch);进行调试。

<?php 
function get_file1($file, $local_path, $newfilename) { 
    $err_msg = ''; 
    echo "<br>Attempting message download for $file<br>"; 
    $out = fopen($newfilename, 'wb'); 
    if ($out == FALSE){ 
      print "File not opened<br>"; 
      exit; 
    } 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_FILE, $out); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_URL, $file); 
    curl_exec($ch); 
    echo "<br>Error is : ".curl_error ( $ch); 
    curl_close($ch); 
    //fclose($handle); 
}
?>

来源: http://www.weberdev.com/get_example.php3?ExampleID=4009

听起来像是你正在尝试用curl做FTP或wget类型的函数。 不确定它与卷曲的效果如何。 选择:

对于 PHP 中的 ftp:

http://php.net/manual/en/book.ftp.php

对于 PHP 的 wget 类功能,您可以尝试

file_get_contents($url)

http://php.net/manual/en/function.file-get-contents.php