0

I use a funtion to download files trough a private API. Everything goes fine to download small/medium files, but large files is impossible beacause it uses too much memory.

Here is my function:

protected function executeFile($method, $url, $params=array(), $as_user=null) {

    $data_string = json_encode($params);
    $method = strtoupper($method);

    $ch = curl_init();

    if($method == 'GET') {
        $url  = $this->options['api_url'].$url.'?';
        $url .= $this->format_query($params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        curl_setopt($ch, CURLOPT_URL, $this->options['api_url'].$url);
    }

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    if($as_user) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string),
        'Token: '.$this->token,
        'As: '.$as_user
        ));
    } else {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string),
        'Token: '.$this->token
        ));
    }

    $result_json = curl_exec($ch);
    $curl_info = curl_getinfo($ch);


    $return             = array();
    $return["result"]   = $result_json;
    $return["entete"]   = $curl_info;
    return $return;
}

How could i optimize this to download files to disk instead of memory ?

Thanks

TheMadCat
  • 107
  • 1
  • 7
  • This question has already been answered here: http://stackoverflow.com/questions/6409462/downloading-a-large-file-using-curl – Erik Kalkoken Apr 14 '16 at 10:32

2 Answers2

1

use CURLOPT_FILE . It will ask for file pointer where download will be saved.

code will be like

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fp = fopen("your_file", 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);

curl_exec ($ch);
vishwakarma09
  • 254
  • 3
  • 17
0

You can use CURLOPT_FILE, like this:

protected function executeFile($method, $url, $params=array(), $as_user=null) {

    $data_string = json_encode($params);
    $method = strtoupper($method);
    $fp = fopen ('savefilepath', 'w+');

    $ch = curl_init();

    if($method == 'GET') {
        $url  = $this->options['api_url'].$url.'?';
        $url .= $this->format_query($params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        curl_setopt($ch, CURLOPT_URL, $this->options['api_url'].$url);
    }

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FILE, $fp); 

    ....

    curl_exec($ch);
    fclose($fp);
    .....
    return $return;
}
Tan Mai Van
  • 657
  • 6
  • 8
  • I've tried this but Firefox tells me that "Source file can not be read"... But file is correcly saved to disk on the server! – TheMadCat Apr 14 '16 at 10:41
  • Just a quick snip code, may be you need to close the file after write to disk: `fclose($fp);`. Check the update for location of the additional code. – Tan Mai Van Apr 14 '16 at 10:44
  • I've tried that, also including a curl_close($ch); but same error and i can't understand why...thanks for your help ! – TheMadCat Apr 14 '16 at 11:13