简体   繁体   中英

PHP: “run” file/script rather than include?

I have a php file that returns html based on certain parameters, but it also saves this output in a separate directory (basically a custom made caching process).

Now I want to build a separate php file that automatically updates the cache based on array of known possible parameters.

So I want to "load" or "run" rather than "include" the file several times with the different parameters so that it will save the results in the cache folder.

Is there a php function that will allow me to simply load this other file and perhaps tell me when it is done? If not, do I need to use ajax for something like this or maybe PHP's curl library??

At the present I was thinking about something along the following lines:

<?php
$parameters = array("option1", "option2", "option3");

//loop through parameters and save to cache folder
foreach ($parameters as $parameter){
   //get start time to calculate process time
   $time_start = microtime(true);
   sleep(1);

   //I wish there was some function called run or load similar to jquery's 'load'
   run("displayindexsearch.php?p=$parameter");

   //return total time that it took to run the script and save to cache
   $time_end = microtime(true);
   $time = $time_end - $time_start;
   echo "Process Time: {$time} seconds";
 }
 ?>

why don't you include the file, but create functions for the things you want to do inside the file. That way, at the time when you want to run, you simply call the function. This seems to be the correct way to do what you are trying to do if I understand it correctly.

The best solution I found is to use:

file_get_contents($url)

So where the questions looks for a replacement for run , I substitute file_get_contents($url) .

Note that I was getting errors when I used a relative path here. I only had success when I used http://localhost/displayindexsearch.php?p=parameter etc.

Please take a look at this question.

Launch php file from php as background process without exec()

It might be helping for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM