2012-07-04 23 views
-1

工作,我試過什麼/我試圖使用捲曲下載一個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

你檢查輸出文件被正確打開? –

回答

1

這應該解決你的問題。

這將下載郵政編碼,而不是他們是空的,這是不久前我的問題的修復。

另外您應該考慮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