从另一个 PHP 脚本执行一个 PHP 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8450696/
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
Execute a PHP script from another PHP script
提问by domino
How would I make my server run a php script by triggering it manually using php? Basically I have a pretty big cronjob file that is ran every 2 hours, but I want to be able to trigger the file manually myself without having to wait for it to load (i want it to be done on the server's side).
如何通过使用 php 手动触发我的服务器运行 php 脚本?基本上我有一个非常大的 cronjob 文件,每 2 小时运行一次,但我希望能够自己手动触发该文件而不必等待它加载(我希望它在服务器端完成)。
EDIT: I want to execute the file from a php file... Not command line.
编辑:我想从 php 文件中执行文件...不是命令行。
回答by Chris Baker
You can invoke a PHP script manually from the command line
您可以从命令行手动调用 PHP 脚本
hello.php
<?php
echo 'hello world!';
?>
Command line:
php hello.php
Output:
hello world!
See the documentation: http://php.net/manual/en/features.commandline.php
请参阅文档:http: //php.net/manual/en/features.commandline.php
EDITOP edited the question to add a critical detail: the script is to be executed by another script.
编辑OP 编辑了问题以添加一个关键细节:脚本将由另一个脚本执行。
There are a couple of approaches. First and easiest, you could simply include the file. When you include a file, the code within is "executed" (actually, interpreted). Any code that is not within a function or class body will be processed immediately. Take a look at the documentation for include
(docs) and/or require
(docs) (note: include_once
and require_once
are related, but different in an important way. Check out the documents to understand the difference) Your code would look like this:
有几种方法。首先也是最简单的方法,您可以简单地包含该文件。当您包含一个文件时,其中的代码被“执行”(实际上,被解释)。任何不在函数或类主体内的代码都将被立即处理。查看include
( docs) 和/或require
( docs) 的文档(注意:include_once
和require_once
是相关的,但在一个重要方面有所不同。查看文档以了解差异)您的代码如下所示:
include('hello.php');
/* output
hello world!
*/
Second and slightly more complex is to use shell_exec
(docs). With shell_exec
, you will call the php binary and pass the desired script as the argument. Your code would look like this:
其次,稍微复杂一点的是使用shell_exec
(docs)。使用shell_exec
,您将调用 php 二进制文件并将所需的脚本作为参数传递。您的代码如下所示:
$output = shell_exec('php hello.php');
echo "<pre>$output</pre>";
/* output
hello world!
*/
Finally, and most complex, you could use the CURL library to call the file as though it were requested via a browser. Check out the CURL library documentation here: http://us2.php.net/manual/en/ref.curl.php
最后,也是最复杂的,您可以使用 CURL 库调用文件,就像通过浏览器请求它一样。在此处查看 CURL 库文档:http: //us2.php.net/manual/en/ref.curl.php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.myDomain.com/hello.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)
$output = curl_exec($ch);
curl_close($ch);
echo "<pre>$output</pre>";
/* output
hello world!
*/
Documentation for functions used
所用函数的文档
- Command line: http://php.net/manual/en/features.commandline.php
include
: http://us2.php.net/manual/en/function.include.phprequire
: http://us2.php.net/manual/en/function.require.phpshell_exec
: http://us2.php.net/manual/en/function.shell-exec.phpcurl_init
: http://us2.php.net/manual/en/function.curl-init.phpcurl_setopt
: http://us2.php.net/manual/en/function.curl-setopt.phpcurl_exec
: http://us2.php.net/manual/en/function.curl-exec.phpcurl_close
: http://us2.php.net/manual/en/function.curl-close.php
- 命令行:http: //php.net/manual/en/features.commandline.php
include
: http://us2.php.net/manual/en/function.include.phprequire
: http://us2.php.net/manual/en/function.require.phpshell_exec
:http: //us2.php.net/manual/en/function.shell-exec.phpcurl_init
:http: //us2.php.net/manual/en/function.curl-init.phpcurl_setopt
:http: //us2.php.net/manual/en/function.curl-setopt.phpcurl_exec
:http: //us2.php.net/manual/en/function.curl-exec.phpcurl_close
:http: //us2.php.net/manual/en/function.curl-close.php
回答by Arnaud
you can use the backtick notation:
您可以使用反引号表示法:
`php file.php`;
You can also put this at the top of the php file to indicate the interpreter:
您也可以将其放在 php 文件的顶部以指示解释器:
#!/usr/bin/php
Change it to where you put php. Then give execute permission on the file and you can call the file without specifying php:
将其更改为您放置 php 的位置。然后给文件执行权限,你可以在不指定php的情况下调用文件:
`./file.php`
If you want to capture the output of the script:
如果要捕获脚本的输出:
$output = `./file.php`;
echo $output;
回答by NightFlight
The OP refined his question to how a php script is called from a script. The php statement 'require' is good for dependancy as the script will stop if required script is not found.
OP 将他的问题提炼为如何从脚本调用 php 脚本。php 语句“require”有利于依赖,因为如果找不到所需的脚本,脚本将停止。
#!/usr/bin/php
<?
require '/relative/path/to/someotherscript.php';
/* The above script runs as though executed from within this one. */
printf ("Hello world!\n");
?>
回答by NightFlight
I prefer to use
我更喜欢使用
require_once('phpfile.php');
lots of options out there for you. and a good way to keep things clean.
有很多选择可供您选择。以及保持事物清洁的好方法。
回答by Jakub
If it is a linux box you would run something like:
如果它是一个 linux 盒子,你会运行类似的东西:
php /folder/script.php
On Windows, you would need to make sure your php.exe file is part of your PATH, and do a similar approach to the file you want to run:
在 Windows 上,您需要确保 php.exe 文件是 PATH 的一部分,并对要运行的文件执行类似的方法:
php C:\folder\script.php
回答by Tom van der Woerdt
Open ssh and execute the command manually?
打开ssh并手动执行命令?
php /path/to/your/file.php
回答by Cfreak
On the command line:
在命令行上:
> php yourfile.php
回答by MiRuS
<?php
$output = file_get_contents('http://host/path/another.php?param=value ');
echo $output;
?>
回答by G. Gréczi
Possible and easiest one-line solution is to use:
可能且最简单的单行解决方案是使用:
file_get_contents("YOUR_REQUESTED_FILE");
Or equivavelt for example CURL.
或 equivavelt 例如 CURL。
回答by himani bisht
Try this:
尝试这个:
header('location: xyz.php'); //thats all for redirecting to another php file