我想出了一个解决方案:
// 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;
}
处理GZIP文件有点不同:
// 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);
}
综上所述。使用wp\\u remote\\u get()检索远程文件时,请使用内置的PHP文件函数,然后将其保存到您想要的位置。如果您有一个利用Wordpress功能的解决方案,请将其发布。