The dynamic content of PHP, its storage and execution daily, and not every page load?

I am looking to reproduce some content that is compiled from the source using a php class. There is no other way to get this content (it's just text).

Now the content is constantly changing, and therefore the function by default gets access and echoes it on every page load, however I would prefer to just refresh it once a day, but continue to repeat the contents of each page load.

So my problem is that the class file is encoded, so I cannot make changes this way. I was hoping that I could create something that would save the text and repeat the saved text until the next day when it starts the original function to get new content.

Function that collects and echo images:

<?php showcontent($txtonly); ?> 

I would also prefer not to put the contents in the file and get its contents. Hope someone can help me :)

+6
source share
4 answers

When you extract it from the source, write a text file. On each page load, check if the file is older than one day. If so, use the usual tools to extract new content and save it to a file. If it is not older than one day, just read the local cache file and print it:

 $cache_file = '/path/to/file.txt'; // Page was requested.... // It less than a day old, just output it. if (file_exists($cache_file) && filemtime($cache_file) > time() - 86400) { echo file_get_contents($cache_file); } // It older than a day, get the new content else { // do whatever you need to get the content $content = whatever(); // Save it into the cache file file_put_contents($cache_file, $content); // output the new content echo $content; } 

Note. You will need to make sure that the directory in which the cache file is stored is writable by your web server.

+8
source

PHP is not "persistent" in the way you need it. You can easily run a PHP script via cron at a certain time to get your changed data, but you must store this data somewhere, otherwise it will disappear when this sample script is completed. The easiest way is to just use the file. A critical choice can fill it with new data, and your other scripts simply include / download this file every time you start it and automatically collect new data every time they change.

+3
source

What you need is a caching component. First you use PHP native output control to capture all the showcontent generated ... further, you store this in a separate file daily (something as simple as cache.php ). It can be as simple as a script-based cron , which starts at 03:01 in the morning, deleting the old file and creating a new one.

Script generation example (called once a day)

 <?php $cache_file="cache.php"; $heredoc_sep="START_TO_END_12";//12 just a number to keep it unlikely change as you see fit (must not ever be in CONTENT below) //Capture content ob_start(); showcontent($txtonly); $content=ob_get_contents(); ob_end_clean(); //Store in file $fp=fopen($cache_file,"w");//Truncates to zero length fwrite($fp,"<?php\necho <<<$heredoc_sep\n"); fwrite($fp,$content); fwrite($fp,"$heredoc_sep;\n"); fclose($fp); ?> 

Now, to use the results, simply <?php include("cache.php");?> Instead of <?php showcontent($txtonly); ?> <?php showcontent($txtonly); ?>

+3
source

In addition to the other answers: If you cannot get your content from the encoded class as a string, you can use the output management functions like this:

 ob_start(); showcontent($txtonly); $content = ob_get_clean(); 
+2
source

Source: https://habr.com/ru/post/895977/