wp_remote_get(), downloading and saving files

Question

From what I can see wp_remote_get() saves remote file contents to memory.

The files that I need to download are compressed in either ZIP or GZIP and within which will either be a CVS or XMl file

What I need to do first is download the remote file to the harddrive as ZIP either GZIP and then unzip them

Is it possible to use wp_remote_get() to download the whole file and save it to a directory?

A non-Wordpress solution I used before was cURL:

public function grab_file($url, $new_file) {

    //get file
    $ch = curl_init();
    $fp = fopen(DIR_PATH."zip/$new_file", "w");

    $options = array(CURLOPT_URL => $url, CURLOPT_HEADER => 0, CURLOPT_FAILONERROR =>
        1, CURLOPT_AUTOREFERER => 1, CURLOPT_BINARYTRANSFER => 1, CURLOPT_RETURNTRANSFER =>
        1, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_TIMEOUT => 5, CURLOPT_FILE => $fp);

    curl_setopt_array($ch, $options);
    $file = curl_exec($ch);
    curl_close($ch);
    fclose($fp);

    if (!$file) {
        //$error = "cURL error number:" . curl_errno($ch);
        //$error .= "cURL error:" . curl_error($ch);
        return false;

    } else {

        return true;

    }
}
0
AndyW 4 years 2020-02-19T08:39:18-05:00 0 Answers 124 views 0

Leave an answer

Browse
Browse