我想使用这个脚本来下载每 20 分钟更新一次的 XML 提要。问题是文件名有日期和时间戳。有人可以指出正确的方向如何更改它,以便它始终选择最新的文件,以便我可以更新库存。
示例文件名:
Feed2842020175940.xml
Feed2842020173907.xml
Feed2842020171807.xml
从步骤 1 的“从 URL 下载”选项调用该函数:
[custom_file_download("ftp://username:password@hostname.com/path/to/file.xml","xml")]
函数本身:
// Programmatically download and return import file via URL.
function custom_file_download($url, $type = 'xml'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
/* Optional: Set headers...
* $headers = array();
* $headers[] = "Accept-Language: de";
* curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
*/
$result = curl_exec($ch);
if (curl_errno($ch)) {
exit('Error:' . curl_error($ch));
}
curl_close ($ch);
$uploads = wp_upload_dir();
$filename = $uploads['basedir'] . '/' . strtok(basename($url), "?") . '.' . $type;
if (file_exists($filename)){
@unlink($filename);
}
file_put_contents($filename, $result);
return str_replace($uploads['basedir'], $uploads['baseurl'], $filename);
}