[英]Unable to Download Remote File from server after using sleep in php
嗨,我在下载远程文件时遇到了一个奇怪的问题。 我正在使用Flurry Data API下载一些报告。 就是在第一次调用Flurry API时,它会向我们提供XML / JSON响应,其中包含下载报告的路径。 准备报告大约需要2分钟。 我对此事有疑问。 我编写了一个脚本,如果我只是将Report的链接直接粘贴到已经准备好下载的函数中,则会下载远程文件。 它的工作原理很吸引人,但我必须自动执行下载过程。 因此,为此,我首先调用API并获取报告下载链接,然后使用PHP的sleep()
函数停止执行3分钟(也尝试了2分钟)。 然后,我调用用于成功下载报告的相同函数,但这次不起作用。 这是文件下载方法:
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
但是它也不起作用。
请求Flurry的代码请注意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()
或我被卡住的问题是第二晚还是无法实现吗?
检查您的PHP脚本是否正在超时并被杀死。 Web服务器和PHP都有最大执行限制,以防止脚本失控,如果您的睡眠时间超过该限制,它将永远不会超过该限制。
http://www.php.net/manual/zh/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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.