使用卷曲下载一个zip文件不与后续的PHP代码

问题描述:

工作,我试过什么/我试图使用卷曲下载一个zip文件不与后续的PHP代码

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不同的事情?

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

+0

你检查输出文件被正确打开? –

听起来像你正在试图做一个FTP或wget类型的函数与卷曲。不知道它如何与卷曲一起工作。替代方案:

对于FTP在PHP中:

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

对于用PHP功能wget的,你可以尝试

的file_get_contents($网址)

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

+0

k了解np – Tucker

这应该解决你的问题。

这将下载邮政编码,而不是他们是空的,这是不久前我的问题的修复。

另外您应该考虑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