WP_Remote_Get(),下载和保存文件

时间:2012-04-24 作者:AndyW

据我所见,wp\\u remote\\u get()将远程文件内容保存到内存中。

我需要下载的文件以ZIP或GZIP格式压缩,其中包含CVS或XMl文件

我首先需要做的是将远程文件作为ZIP或GZIP下载到硬盘上,然后解压缩它们

是否可以使用wp\\u remote\\u get()下载整个文件并将其保存到目录中?

我之前使用的非Wordpress解决方案是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;

    }
}

2 个回复
SO网友:AndyW

我想出了一个解决方案:

// 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功能的解决方案,请将其发布。

SO网友:Daan van den Bergh

虽然这个问题是几年前提出来的,但因为它是谷歌的第一个搜索结果之一,所以我想我应该给出一个更好的建议来写wp_remote_get()\'s对文件的响应。

开始很好,但OP使用fopen()fwrite() 要写入文件,尽管这不是必需的:

// Use wp_remote_get to fetch the data
$data = wp_remote_get($url);

// Save the body part to a variable
$zip = $data[\'body\'];

// Then the unzipping part...

// Create the name of the file and the declare the directory and path
$file = \'/path/to/file.zip\';

// Write the file using put_contents instead of fopen(), etc.
global $wp_filesystem;

$wp_filesystem->put_contents($file, $zip);
在某些情况下$wp_filesystem 无法访问。在这种情况下,您可以创建一个函数来获取它:

function filesystem()
{
    global $wp_filesystem;

    if ( is_null( $wp_filesystem ) ) {
        require_once ABSPATH . \'/wp-admin/includes/file.php\';
        WP_Filesystem();
    }

    return $wp_filesystem;
}

结束

相关推荐