ob_start
素晴らしい解決策ではないでしょう。これは、出力バッファーを変更またはフラッシュする必要がある場合にのみ適用されます。XML で返されたデータはバッファに送信されていないため、これらの呼び出しは必要ありません。
これは、私が過去に使用した1つのソリューションです。データはフラット ファイルに保存されるため、MySQL やデータベースは必要ありません。
$last_cache = -1;
$last_cache = @filemtime( 'weather_cache.txt' ); // Get last modified date stamp of file
if ($last_cache == -1){ // If date stamp unattainable, set to the future
$since_last_cache = time() * 9;
} else $since_last_cache = time() - $last_cache; // Measure seconds since cache last set
if ( $since_last_cache >= ( 3600 * 5) ){ // If it's been 5 hours or more since we last cached...
$url = 'http://w1.weather.gov/xml/current_obs/KGJT.xml'; // Pull in the weather
$xml = simplexml_load_file($url);
$weather = $xml->weather . " " . $xml->temperature_string;
$fp = fopen( 'weather_cache.txt', 'a+' ); // Write weather data to cache file
if ($fp){
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, "\r\n" . $weather );
flock($fp, LOCK_UN);
}
fclose($fp);
}
}
include_once('weather_cache.txt'); // Include the weather data cache