Well here’s the solution that I came up with:
// Use wp_remote_get to fetch the data
$response = wp_remote_get($url);
// Save the body part to a variable
$zip = $data['body'];
// In the header info is the name of the XML or CVS file. I used preg_match to find it
preg_match("/.datafeed_([0-9]*)\../", $response['headers']['content-disposition'], $match);
// Create the name of the file and the declare the directory and path
$file = DIR_PATH."zip/"."datafeed_".$match[1].".zip";
// Now use the standard PHP file functions
$fp = fopen($file, "w");
fwrite($fp, $zip);
fclose($fp);
// to unzip the file use the WordPress unzip_file() function
// You MUST declare WP_Filesystem() first
WP_Filesystem();
if (unzip_file($file, DIR_PATH."feeds")) {
// Now that the zip file has been used, destroy it
unlink($file);
return true;
} else {
return false;
}
Dealing with a GZIP file was a bit different:
// It is necessary to use the raw URL and find the extension of the encluse XML or CVS file first
private function save_ungzip($data, $url, $ext) {
// As with ZIP, save the body of wp_remote_get() to memory
$gzip = $data['body'];
// As with ZIP, look for the name of the file in the header
preg_match("/.datafeed_([0-9]*)\../", $data['headers']['content-disposition'], $match);
$file = DIR_PATH."feeds/"."datafeed_".$match[1].".$ext";
// now you need to use both the PHP gzip functions and the PHP file functions
$remote = gzopen($url, "rb");
$home = fopen($file, "w");
while ($string = gzread($remote, 4096)) {
fwrite($home, $string, strlen($string));
}
gzclose($remote);
fclose($home);
}
So in conclusion. When you use wp_remote_get() to retrieve a remote file use the inbuilt PHP file functions to then save it where you want to. If you have a solution that utilizes WordPress functions then please post it up.