0

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.

Seeker
  • 1,877
  • 4
  • 32
  • 56

1 Answers1

0

Check if your PHP script is timing out and being killed off. Both the webserver and PHP have max execution limits to prevent runaway scripts, and if your sleep surpasses that limit, it'll never continue beyond that.

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

SpenserJ
  • 802
  • 5
  • 13
  • Thanks for the reply actually i set the max execution time as well to unlimited and for memory problem i have set it to max as well using: `ini_set('memory_limit', '-1'); ini_set("max_execution_time", "0");` Thats why i was curious why it is happening – Seeker Jun 21 '13 at 17:19
  • Try adding an echo after your sleep to ensure that it is reaching that point. Assuming that is being reached, I'd look at `if($http_response_header[3] == "Content-Type: application/octet-stream"){`. The order of the response headers may not always be the same, so you may end up checking the wrong header. – SpenserJ Jun 21 '13 at 17:37
  • i tried it as well it is coming back i printed the response header they are getting printed and actually the header is always same i.e. `Content-Type: application/octet-stream` i tried it even echoing inside that condition it is working and i am positive about that as ewll – Seeker Jun 21 '13 at 17:51