嗨,我有一個奇怪的問題,下載遠程文件。我正在使用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()
還是什麼我堅持它一直是第二晚,我不能實現這一目標。
的是它甚至不顯示在捲曲錯誤的任何錯誤呼應部分 – Seeker