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文件执行文件...不是命令行。
10 个解决方案
#1
86
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
EDIT OP 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:
有几种方法。首先也是最简单的,您可以简单地包含该文件。当您包含文件时,其中的代码将被“执行”(实际上,已解释)。任何不在函数或类体内的代码都将立即处理。看看包含(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
- 命令行:http://php.net/manual/en/features.commandline.php
-
include
: http://us2.php.net/manual/en/function.include.php - 包括:http://us2.php.net/manual/en/function.include.php
-
require
: http://us2.php.net/manual/en/function.require.php - 要求:http://us2.php.net/manual/en/function.require.php
-
shell_exec
: http://us2.php.net/manual/en/function.shell-exec.php - shell_exec:http://us2.php.net/manual/en/function.shell-exec.php
-
curl_init
: http://us2.php.net/manual/en/function.curl-init.php - curl_init:http://us2.php.net/manual/en/function.curl-init.php
-
curl_setopt
: http://us2.php.net/manual/en/function.curl-setopt.php - curl_setopt:http://us2.php.net/manual/en/function.curl-setopt.php
-
curl_exec
: http://us2.php.net/manual/en/function.curl-exec.php - curl_exec:http://us2.php.net/manual/en/function.curl-exec.php
-
curl_close
: http://us2.php.net/manual/en/function.curl-close.php - curl_close:http://us2.php.net/manual/en/function.curl-close.php
#2
5
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");
?>
#3
3
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;
#4
2
I prefer to use
我更喜欢使用
require_once('phpfile.php');
lots of options out there for you. and a good way to keep things clean.
有很多选择供你使用。以及保持清洁的好方法。
#5
0
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
#6
0
On the command line:
在命令行上:
> php yourfile.php
#7
0
Open ssh and execute the command manually?
打开ssh并手动执行命令?
php /path/to/your/file.php
#8
0
to "chain" another php program from within your code, use "header"
从你的代码中“链接”另一个php程序,使用“header”
header("Location:index.php?page=menus");
头:;( “位置index.php页面=菜单?”)
#9
0
<?php
$output = file_get_contents('http://host/path/another.php?param=value ');
echo $output;
?>
#10
-4
I think this is what you are looking for
我想这就是你要找的东西
<?php include ('Scripts/Php/connection.php');
//The connection.php script is executed inside the current file ?>
The script file can also be in a .txt format, it should still work, it does for me
脚本文件也可以是.txt格式,它仍然可以工作,它对我来说
e.g.
例如
<?php include ('Scripts/Php/connection.txt');
//The connection.txt script is executed inside the current file ?>
#1
86
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
EDIT OP 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:
有几种方法。首先也是最简单的,您可以简单地包含该文件。当您包含文件时,其中的代码将被“执行”(实际上,已解释)。任何不在函数或类体内的代码都将立即处理。看看包含(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
- 命令行:http://php.net/manual/en/features.commandline.php
-
include
: http://us2.php.net/manual/en/function.include.php - 包括:http://us2.php.net/manual/en/function.include.php
-
require
: http://us2.php.net/manual/en/function.require.php - 要求:http://us2.php.net/manual/en/function.require.php
-
shell_exec
: http://us2.php.net/manual/en/function.shell-exec.php - shell_exec:http://us2.php.net/manual/en/function.shell-exec.php
-
curl_init
: http://us2.php.net/manual/en/function.curl-init.php - curl_init:http://us2.php.net/manual/en/function.curl-init.php
-
curl_setopt
: http://us2.php.net/manual/en/function.curl-setopt.php - curl_setopt:http://us2.php.net/manual/en/function.curl-setopt.php
-
curl_exec
: http://us2.php.net/manual/en/function.curl-exec.php - curl_exec:http://us2.php.net/manual/en/function.curl-exec.php
-
curl_close
: http://us2.php.net/manual/en/function.curl-close.php - curl_close:http://us2.php.net/manual/en/function.curl-close.php
#2
5
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");
?>
#3
3
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;
#4
2
I prefer to use
我更喜欢使用
require_once('phpfile.php');
lots of options out there for you. and a good way to keep things clean.
有很多选择供你使用。以及保持清洁的好方法。
#5
0
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
#6
0
On the command line:
在命令行上:
> php yourfile.php
#7
0
Open ssh and execute the command manually?
打开ssh并手动执行命令?
php /path/to/your/file.php
#8
0
to "chain" another php program from within your code, use "header"
从你的代码中“链接”另一个php程序,使用“header”
header("Location:index.php?page=menus");
头:;( “位置index.php页面=菜单?”)
#9
0
<?php
$output = file_get_contents('http://host/path/another.php?param=value ');
echo $output;
?>
#10
-4
I think this is what you are looking for
我想这就是你要找的东西
<?php include ('Scripts/Php/connection.php');
//The connection.php script is executed inside the current file ?>
The script file can also be in a .txt format, it should still work, it does for me
脚本文件也可以是.txt格式,它仍然可以工作,它对我来说
e.g.
例如
<?php include ('Scripts/Php/connection.txt');
//The connection.txt script is executed inside the current file ?>