2010-11-23 56 views
0

我有一種情況,即一個公司給我提供一個XML文件有以下限制:擷取及自託管的XML供稿

  1. 用戶名/密碼保護
  2. 存儲在他們的ftp
  3. 我「M不允許從我的應用程序中引用的文件

所以我希望能拿出東西來獲取XML文件每隔一小時(作爲其文件每小時更新一次),並託管到我自己的域名。

會有人對創建這樣的事情有任何建議,或有任何現有的腳本,可能會爲我做?

非常感謝, 格倫

+1

哪種語言(PHP,.NET C#等)/? ...文件有多大? – 2010-11-23 01:38:39

回答

1

你可以使用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;