开发者

Fetching & Self-hosting an XML Feed

I have a situation whereby a company is provi开发者_运维百科ding 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


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:password@server.domain.com/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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜