PHP:“;运行“;文件/脚本,而不是include


PHP: "run" file/script rather than include?

我有一个php文件,它根据某些参数返回html,但它也将此输出保存在一个单独的目录中(基本上是一个自定义的缓存过程)。

现在,我想构建一个单独的php文件,根据已知的可能参数数组自动更新缓存。

所以我想用不同的参数多次"加载"或"运行"文件,而不是"包含"文件,这样它就会将结果保存在缓存文件夹中。

有没有php函数可以让我简单地加载另一个文件,并告诉我什么时候完成?如果没有,我是否需要使用ajax来实现类似的功能,或者PHP的curl库??

目前,我正在思考以下几点:

<?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";
 }
 ?>

为什么不包含该文件,而是为您想在文件中执行的操作创建函数。这样,当您想要运行时,只需调用函数即可。如果我理解正确的话,这似乎是做你想做的事情的正确方法。

我发现的最佳解决方案是使用:

file_get_contents($url)

因此,在问题寻找run的替代品的地方,我用file_get_contents($url)代替。

请注意,当我在这里使用相对路径时,会出现错误。我只有在使用http://localhost/displayindexsearch.php?p=parameter等时才成功。

请看一下这个问题。

从php启动php文件作为后台进程,无需执行()

这可能对你有帮助。