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).
EDIT: I want to execute the file from a php file... Not command line.
You can invoke a PHP script manually from the command line
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
EDIT OP edited the question to add a critical detail: the script is to be executed by another script.
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('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:
$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
$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
include
: http://us2.php.net/manual/en/function.include.php require
: http://us2.php.net/manual/en/function.require.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_setopt
: http://us2.php.net/manual/en/function.curl-setopt.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 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:
#!/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:
`./file.php`
If you want to capture the output of the script:
$output = `./file.php`;
echo $output;
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.
#!/usr/bin/php
<?
require '/relative/path/to/someotherscript.php';
/* The above script runs as though executed from within this one. */
printf ("Hello world!\n");
?>
I prefer to use
require_once('phpfile.php');
lots of options out there for you. and a good way to keep things clean.
<?php
$output = file_get_contents('http://host/path/another.php?param=value ');
echo $output;
?>
I think the function "eval" is the right solution.
Official docs here: https://www.php.net/manual/en/function.eval.php
And the code should be of the following form:
$some_code = retrieve_or_generate_some_php_code();
eval($some_code);
If it is a linux box you would run something like:
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:
php C:\folder\script.php
打开ssh并手动执行命令?
php /path/to/your/file.php
从你的代码中“链接”另一个php程序,使用“header”
header("Location:index.php?page=menus");
在命令行上:
> php yourfile.php
Possible and easiest one-line solution is to use:
file_get_contents("YOUR_REQUESTED_FILE");
Or equivavelt for example CURL.
尝试这个:
header('location: xyz.php'); //thats all for redirecting to another php file
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
eg
<?php include ('Scripts/Php/connection.txt');
//The connection.txt script is executed inside the current file ?>
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.