php 如何让php脚本运行另一个php脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1110080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make php script run another php script
提问by Phil
I'm trying to do exactly the same thing as in my previous Python question, but in PHP. See my previous (answered) question
我正在尝试做与我之前的 Python 问题完全相同的事情,但是在 PHP 中。查看我之前的(已回答)问题
PHP script from previous question does something and then runs another PHP script on the same server, does something and then quits (while second script still continues its work). How do I achieve this?
上一个问题中的 PHP 脚本做了一些事情,然后在同一台服务器上运行另一个 PHP 脚本,做了一些事情然后退出(而第二个脚本仍在继续工作)。我如何实现这一目标?
Please note that PHP script is also a web page at the same time (so maybe we can use it like in previous question where answer to my question was snippet that made python just open urlinstead of running subprocess... although I don't have a clue if that's useful information, maybe it's different in PHP, I'm not too experienced in PHP)... and that I want to make each scripts independent - so if first php script will finish I would like second php script to continue working even though first one ended.
请注意,PHP 脚本同时也是一个网页(所以也许我们可以像在上一个问题中那样使用它,其中我的问题的答案是使 python 只打开 url而不是运行子进程的代码段......虽然我没有有一个线索,如果这是有用的信息,也许它在 PHP 中有所不同,我在 PHP 方面不太有经验)......而且我想让每个脚本独立 - 所以如果第一个 php 脚本将完成我想要第二个 php 脚本即使第一个结束,也要继续工作。
What do you think is most elegant way to do this? Would echoing iframe work or should I do this differently?
你认为最优雅的方式是什么?回显 iframe 会起作用还是应该以不同的方式执行此操作?
回答by Tom Haigh
This is a bit of unix/linux-only hack that might not work on shared web servers:
这是一些可能不适用于共享 Web 服务器的 unix/linux-only hack:
file1.php
文件1.php
<?php
$somearg = escapeshellarg('blah');
exec("php file2.php $somearg > /dev/null &");
file2.php
文件2.php
<?php
//do some stuff that will take a while
//$argv should contain 'blah', and also it seems the name of the php file
//this script will continue to run. You might want to set max_execution_time to a sensible value.
回答by Unknown
You can do it by placing
header('Location: second.php');at the end of first.php and again at the end of second.php to start executing third.php and so on. Even you can create cycles between two scripts, i.e after ending first.php, second.php will be started and after executing second.php again the first.php, again second.php and so on.
你可以通过放在
header('Location: second.php');first.php 的末尾然后再次放在 second.php 的末尾来开始执行third.php 等等。即使你可以在两个脚本之间创建循环,即在结束 first.php 后,second.php 将被启动,在再次执行 second.php 后,first.php,再次 second.php 等等。
But remember, this is a browser dependent feature and how many script can be executed one after another, totally depends on how many times that browser permits redirection. I found mozilla allows 23 redirections, that means you can executes 23 scripts one after another.
但请记住,这是一项依赖于浏览器的功能,可以一个接一个执行多少个脚本,完全取决于浏览器允许重定向的次数。我发现 mozilla 允许 23 个重定向,这意味着您可以一个接一个地执行 23 个脚本。
回答by Jinu Joseph Daniel
To run a second script on the same server
在同一台服务器上运行第二个脚本
$appUrl = $_SERVER['HTTP_HOST'];
$path = 'second.php';//your path here
$appUrl = 'http://'.$appUrl.'/'.$path;
file_get_contents($appUrl);
回答by Justin Giboney
回答by Matchu
If you needthis other script to be run, depending on the client would not be wise.
如果您需要运行其他脚本,则取决于客户端是不明智的。
What I'd do is use the technique described in the answer to this StackOverflow question(which points to this comment in the PHP documentation) and include the other script as your post-processing.
我要做的是使用此 StackOverflow 问题的答案中描述的技术(指向PHP 文档中的此评论),并将其他脚本包含在您的后处理中。
However, that comment was written in 2006, and things may have changed since then. Please give the technique a try (as I will be doing, just for fun) and see if it works for you :)
然而,该评论是在 2006 年写的,从那时起事情可能已经发生了变化。请尝试一下该技术(正如我将要做的,只是为了好玩),看看它是否适合您:)
回答by EvilChookie
To the best of my knowledge, there isn't any way to run a second page (AJAX is the exception). As far as I know, PHP doesn't support multiple threads (please correct me if I'm wrong) and the 'single-thread' nature of the web seems to defeat it anyways.
据我所知,没有任何方法可以运行第二页(AJAX 是例外)。据我所知,PHP 不支持多线程(如果我错了,请纠正我)并且网络的“单线程”性质似乎无论如何都打败了它。
I would rather be inclined to look at your specific application and find out why you need to have two separate pages run - and then re-engineer the process so it does not.
我宁愿倾向于查看您的特定应用程序并找出为什么您需要运行两个单独的页面 - 然后重新设计流程,使其不需要。
I'd be willing to bet that a re-engineer would end being less of a headache from a development standpoint, as well as a logic and implementation one.
我敢打赌,从开发的角度以及逻辑和实现的角度来看,再工程师最终不会那么令人头疼。
回答by shadowhand
You should look into pcntl_forkif you want a multithreaded application. Also look at ignore_user_abort.
如果您想要一个多线程应用程序,您应该查看pcntl_fork。另请查看ignore_user_abort。
回答by Matchu
I'm having trouble getting my previous answer to work, though I suspect it may be the fault of my own server, or perhaps new browsers refusing the close the connection when instructed (I'm really not a pro on how that shtuff works).
我无法让我以前的答案工作,尽管我怀疑这可能是我自己的服务器的错,或者新的浏览器在收到指示时拒绝关闭连接(我真的不是这个 shtuff 工作原理的专业人士) .
If that method doesn't work for you, either, try this article on pseudo-multi-threading in PHP, and see if you have better luck :)
如果该方法对您不起作用,请尝试这篇关于 PHP 中的伪多线程的文章,看看您是否有更好的运气:)
回答by Taras Soroka
If your remote script is accessible as simple webpage via http, you might try variant with http get request where response timeout set to minimal time for example via CURL:
如果您的远程脚本可以通过 http 作为简单网页访问,您可以尝试使用 http get 请求的变体,其中响应超时设置为最短时间,例如通过 CURL:
if( $curl = curl_init() ) {
curl_setopt($curl, CURLOPT_URL, 'http://your_site/your_script.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_exec($curl);
curl_close($curl);
}
but so you will have delay in 1 second in the your first script execution.
但是因此您将在第一个脚本执行中延迟 1 秒。

