dongpigui8898 2013-06-21 17:05
浏览 70

在php中使用sleep后无法从服务器下载远程文件

Hi there i am having a strange problem with downloading a remote file. I am using Flurry Data API to download some Reports. Thing is when first time we call the Flurry API it gives us XML/JSON response which contains the path of the Report for Download. It takes like 2 minutes for the report to get ready. I am having Problem with that thing. I wrote a Script which download the remote file if i just paste the link of Report directly to function which is already ready to download. It works like a charm but i have to automate the process of Downloading. So for that i First call the API and get the Report Download Link then i use sleep() function of PHP to stop execution for like 3 Minutes(Tried it with 2 also). Then i call the same function which i uses to download the report successfully doesn't work this time. Here is the File Download Method:

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);
}

i also have tried giving the CURLOPT_TIMEOUT but it wasn't working either.

The code to Request to the Flurry note that download_path is working properly it just get the report link:

$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";
    }

is there something problem with sleep() or what i am stuck its been 2nd night i am unable to achieve it.

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害