D
D
dmitrypix2016-07-24 20:43:42
JSON
dmitrypix, 2016-07-24 20:43:42

How to cache json using php?

Tell me how to cache a JSON file, the file is connected from another site, and must first go to the tmp folder and then be processed from it.

$file = 'Внешний сайт/json/liveu.json';

$cache_path = "tmp";       // папка с кэш-файлами на сервере
$cache_file = sprintf($file, $cache_path);
$cache_ttl = 900;            // время жизни кэша в секундах
$html = '';                  // HTML код для вставки на страницу

if( file_exists( $cache_file)  &&  (time() - filemtime($cache_file)) < $cache_ttl) {
    // берём кэшированные данные
    $html = file_get_contents( $cache_file);
} else {
   $html = 2;
}
echo $html;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2016-07-24
@zoonman

php.net/manual/en/function.file-put-contents.php

$html = file_get_contents($file);
 file_put_contents($cache_file, $html);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question