获取&自托管 XML Feed

发布于 2024-10-04 01:39:01 字数 255 浏览 4 评论 0原文

我遇到过这样一种情况,一家公司向我提供了一个带有以下限制的 xml 文件:

  1. 用户名/密码受保护
  2. 存储在他们的 ftp 上
  3. 我不允许从我的应用程序中引用该文件

所以我希望能想出一些方法来获取每小时更新一次 xml 文件(因为他们的文件每小时更新一次),并将其托管在我自己的域上。

有人对创建这样的东西有任何建议吗?或者是否有任何现有脚本可以为我做这件事?

非常感谢, 格伦

I have a situation whereby a company is providing me an xml file with the following restrictions:

  1. Username/Password protected
  2. Stored on their ftp
  3. I'm not allowed to reference the file from my application

So I was hoping to come up with something to fetch the xml file every hour (as their file is updated hourly), and host it on my own domain.

Would anyone have any suggestions about creating something like this, or is there any existing scripts that might do it for me?

Many thanks,
Glen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2024-10-11 01:39:01

您可以使用简单的 PHP 来缓存远程文件并提供本地副本。

基于这个 PHP Remote File Cache 示例,你可以这样做(未经测试):

<?php
$url = 'ftp://username:[email protected]/folder/file.ext';
#we need to do some caching here
$cache_dir = dirname(__FILE__) . '/cache/'; // directory to store the cache
$cache_file = $cache_dir . md5($url);
$cache_time = 1 * 60 * 60; // time to cache file, # minutes * seconds

// check the cache_dir variable
if (is_dir($cache_dir) && 
    is_writable($cache_dir) && 
    file_exists($cache_file) && 
    (time() - $cache_time) < filemtime($cache_file)) {
    $data = file_get_contents($cache_file); // name of the cached file
}
else {
    $data = file_get_contents($url);
    file_put_contents($cache_file, $data); //go ahead and cache the file
}


// Compress output if we can.
if (function_exists('ob_gzhandler')) {
    ob_start('ob_gzhandler');
}


header('Content-type: text/xml; charset=UTF-8'); // Change this as needed
// think about client side caching...
header('Cache-Control: must-revalidate');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_time) . ' GMT');
echo $data;
exit;

You could use a simple bit of PHP to cache the remote file and serve up the local copy.

Based on this PHP Remote File Cache example you could do something like this (untested):

<?php
$url = 'ftp://username:[email protected]/folder/file.ext';
#we need to do some caching here
$cache_dir = dirname(__FILE__) . '/cache/'; // directory to store the cache
$cache_file = $cache_dir . md5($url);
$cache_time = 1 * 60 * 60; // time to cache file, # minutes * seconds

// check the cache_dir variable
if (is_dir($cache_dir) && 
    is_writable($cache_dir) && 
    file_exists($cache_file) && 
    (time() - $cache_time) < filemtime($cache_file)) {
    $data = file_get_contents($cache_file); // name of the cached file
}
else {
    $data = file_get_contents($url);
    file_put_contents($cache_file, $data); //go ahead and cache the file
}


// Compress output if we can.
if (function_exists('ob_gzhandler')) {
    ob_start('ob_gzhandler');
}


header('Content-type: text/xml; charset=UTF-8'); // Change this as needed
// think about client side caching...
header('Cache-Control: must-revalidate');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_time) . ' GMT');
echo $data;
exit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文