2013-06-21 42 views
0

嗨,我有一個奇怪的問題,下載遠程文件。我正在使用Flurry Data API來下載一些報告。事情是當我們第一次調用Flurry API時,它給了我們XML/JSON響應,其中包含Report for Download的路徑。報告準備工作需要2分鐘的時間。我遇到了那個問題。我寫了一個腳本,下載遠程文件,如果我只是直接將報告鏈接粘貼到已準備好下載的功能上。它像一個魅力,但我必須自動下載的過程。所以對於我首先調用API並獲得報告下載鏈接,然後我使用PHP的sleep()函數停止執行像3分鐘(試用它也是2)。然後我調用我用來成功下載報告的相同功能,這次不起作用。下面是文件下載方法:無法下載遠程文件在服務器使用睡眠後在php

function get_file_and_save($file, $local_path, $newfilename) { 
    $err_msg = '';   
    $out = fopen($local_path . $newfilename . ".gz", 'wb'); 
    if ($out == FALSE) { 
     print "File is not available<br>"; 
     exit; 
    } 

    $ch = curl_init(); 
    $headers = array('Content-type: application/x-gzip', 'Connection: Close'); 
    curl_setopt($ch, CURLOPT_FILE, $out); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_URL, $file); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
    curl_exec($ch); 

    echo "<br>Error Occured:" . curl_error($ch); 

    curl_close($ch); 
} 

我也試圖給CURLOPT_TIMEOUT,但它不工作要麼。

以請求亂舞注意download_path正常工作,它只是得到報告的鏈接代碼:

$query_url = 'http://api.flurry.com/rawData/Events?apiAccessCode=' . $ACCESS_CODE . '&apiKey=' . $API_KEY . '&startTime=' . $start_time . '&endTime=' . $end_time;  

    $response = download_path($query_url); 

    if ($response) { 
     $response_obj = json_decode($response, true); 

     if (isset($response_obj['report']['@reportUri'])) { 
      $report_link = $response_obj['report']['@reportUri']; 
     } 

     if (isset($response_obj['report']['@reportId'])) { 
      $report_id = $response_obj['report']['@reportId']; 
     } 

     if(isset($report_link) && !empty($report_link)){   
       echo "<br >Report Link: ".$report_link."<br >"; 
       sleep(240); 

       $config = array(
        'http' => array(
         'header' => 'Accept: application/json', 
         'method' => 'GET' 
        ) 
       ); 
       $stream = stream_context_create($config); 
       $json= file_get_contents($report_link,false,$stream); 
       echo "<pre>"; 
       print_r($http_response_header); 
       echo "</pre>";  

       if($http_response_header[3] == "Content-Type: application/octet-stream"){ 
        get_file_and_save($report_link, "data-files/gz/", $current_time); 
       } 
      } 
      else{   
       echo "There was some error in downloading report"; 
      }   

    } else { 

     $error = true; 
     echo "There was some error in genrating report"; 
    } 

是有什麼問題sleep()還是什麼我堅持它一直是第二晚,我不能實現這一目標。

+0

的是它甚至不顯示在捲曲錯誤的任何錯誤呼應部分 – Seeker

回答

0

檢查您的PHP腳本是否超時並被關閉。網絡服務器和PHP都有最大的執行限制,以防止失控的腳本,如果你的睡眠超過了這個限制,它永遠不會超越這個限制。

http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time
http://php-fpm.org/wiki/Configuration_File - request_terminate_timeout

http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_read_timeout
http://httpd.apache.org/docs/2.0/mod/core.html#timeout

+0

感謝您的答覆其實我設置最長執行時間以及無限的和內存的問題,我已經把它設置爲最大以及使用: 'ini_set('memory_limit','-1'); ini_set(「max_execution_time」,「0」);'這就是爲什麼我好奇它爲什麼會發生 – Seeker

+0

嘗試在睡眠之後添加echo以確保它達到那個點。假設正在達到,我會看'if($ http_response_header [3] ==「Content-Type:application/octet-stream」){'。響應標題的順序可能並不總是相同的,所以你最終可能會檢查錯誤的標題。 – SpenserJ

+0

我也試過它,它回來了,我打印了他們正在打印的響應頭,實際上頭部總是相同的,即'Content-Type:application/octet-stream'我試過它甚至在它正在工作的條件下回顯,我對此表示肯定 – Seeker

相關問題