CURL RETURNTRANSFER下载大文件

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

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

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