64

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.

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
domino
  • 7,271
  • 12
  • 36
  • 48
  • 2
    Just run the same command the cron is? Or just do `php file.php`? – gen_Eric Dec 09 '11 at 19:31
  • 1
    Why don't you just copy the command from the crontab and paste it into the command line? – Ben Lee Dec 09 '11 at 19:32
  • @Rocket I want to do it from a php file – domino Dec 09 '11 at 19:35
  • 3
    Thanks for 3 down votes and 6 incorrect answers. Thanks for nothing. – domino Dec 09 '11 at 19:49
  • 13
    The answers were only incorrect because you didn't include vital details in your question. Given the details you provided, the answers are 100% correct. Now that you have described what you're *actually* trying to do, the answers will improve. Very simple, you have no reason to be disgruntled. People are trying to help. – Chris Baker Dec 09 '11 at 19:55
  • For anyone reading this almost 8 years later, saying "I want to run it from a PHP file" is not the same as saying "I want it to run in the browser, not command line" because you can still run it from a PHP file in the command line OR in the browser. – j_allen_morris May 30 '19 at 21:14

12 Answers12

121

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

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
  • 2
    That would have been very useful information to include in your question! :P See the edit, I discuss the numerous ways to call one PHP script from another. – Chris Baker Dec 09 '11 at 19:55
  • 1
    Thanks a lot for the lengthy reply! – domino Dec 09 '11 at 19:58
  • 1
    The critical difference is that interpreting the other script using `include`/`require` will run it in the original script’s environment. That means with all its globals, ini settings etc. Running as a shell script or through a HTTP request ensures that the other script runs safely in its own separate environment without interfering with the original one’s. – Glutexo Feb 02 '16 at 06:59
  • Great answer @ChrisBaker , i used your cURL example, but my question to you is : is the file executed on the url secured ? or the whole file is loaded to the current php file? – Zame Aug 12 '16 at 09:51
  • @WalidSarkis I don't know what you mean by "secured." It must be web-accessible, so anyone with the URL could access the same file unless you're calling `localhost` or if you otherwise take measures to prevent access by unauthorized parties. That could be .htaccess, tokens in the URL, session, cookies, web server configuration, etc. It's hard to guess what you mean. – Chris Baker Aug 12 '16 at 14:40
  • @ChrisBaker sry i wasn't specific , what i meant is : when using cURL to access another php file, the php file is executed on the server side or on the client side ? – Zame Aug 16 '16 at 11:40
  • @WalidSarkis The file is executed on the server-side, as if you had used a browser to access the file. The host server needs to have php and a webserver running. – Chris Baker Aug 16 '16 at 15:26
12

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;
Arnaud
  • 410
  • 5
  • 10
  • 2
    Most underrated answer. From PHPs docs: "Use of the backtick operator is identical to shell_exec()." so `$output = \`php file.php\`` should work as well – Solrac Jan 09 '19 at 22:25
8

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");

?>
NightFlight
  • 99
  • 1
  • 4
6

I prefer to use

require_once('phpfile.php');

lots of options out there for you. and a good way to keep things clean.

1
<?php
$output = file_get_contents('http://host/path/another.php?param=value ');
echo $output;
?>
MiRuS
  • 19
  • 1
0

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);
Vova
  • 53
  • 7
0

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
Jakub
  • 20,418
  • 8
  • 65
  • 92
0

Open ssh and execute the command manually?

php /path/to/your/file.php
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
-1

On the command line:

> php yourfile.php
Cfreak
  • 19,191
  • 6
  • 49
  • 60
-2

Possible and easiest one-line solution is to use:

file_get_contents("YOUR_REQUESTED_FILE");

Or equivavelt for example CURL.

G. Gréczi
  • 11
  • 2
  • 1
    This does not execute the PHP script file, it returns the content of the file, ergo - the code written there. – John Jan 27 '21 at 09:06
-3

Try this:

header('location: xyz.php'); //thats all for redirecting to another php file
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
himani bisht
  • 5
  • 1
  • 3
-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

e.g.

<?php include ('Scripts/Php/connection.txt');
//The connection.txt script is executed inside the current file ?>
user2521037
  • 181
  • 1
  • 2