Execute a PHP Script from Another PHP Script

Execute a PHP script from another PHP script

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

  • Command line: http://php.net/manual/en/features.commandline.php
  • 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

How to execute a php script from another php script?

you can use include()

include('file1.php');
include('file2.php');
include('file3.php');

or include_once()

include_once('file1.php');
include_once('file2.php');
include_once('file3.php');

or require or require_once

require 'file1.php';
require 'file2.php';
require 'file3.php';

=> require() will produce a fatal error (E_COMPILE_ERROR) and stop the script

=> include() will only produce a warning (E_WARNING) and the script will continue

How to execute a php script from another php script by using the shell?

If you need to write a php file's output into a variable use the ob_start and ob_get_contents functions. See below:

<?php
ob_start();
include('myfile.php');
$myStr = ob_get_contents();
ob_end_clean();
echo '>>>>' . $myStr . '<<<<';
?>

So if your 'myfile.php' contains this:

<?php
echo 'test';
?>

Then your output will be:

>>>>test<<<<

call php script in another php file

Just use require_once('handler.php') to include the script and run it.

How to execute a php script from another?

If you do not want to wait for them to finish, run them with either

exec('php script.php &> /dev/null &');
shell_exec('php script.php &> /dev/null &');
system('php script.php &> /dev/null &');
`php script.php &> /dev/null &`

Any of those should accomplish the job, depending on your PHPs configuration. Although they are different functions, their behaviour should be similar since all output is being redirected to /dev/null and the proccess is immediately detached.

I use the first solution in a production environment where a client launches a bash SMSs sending script which can take up to 10 minutes to finish, it has never failed.

More info in: http://php.net/exec · http://php.net/shell_exec · http://php.net/system

How do you conditionally call one php script from another with parameters

To pass control to a new script, you could use the header function. using this approach, you'd have to ensure that no other output is being generated by the first script prior to passing control, otherwise header will throw an error.

if($test){
$var = 5;

header('Location: http://www.example.com/2.php?var=' . $var);
}

Then...

2.php
<?php
$varFrom1 = $_GET['var'];

Alternatively, I recommend using the include statement which lets you bring in another file and execute the functions contained therein

include '2.php';

$variable = "something";

if($test){
functionIn2($variable);
}

You can reference it here: http://php.net/manual/en/function.include.php

execute another php file within one php within loop

exec function executes file on server. You won't see anything unless you explicitly get result of exec to a variable and output it.

But as I see - you just want to include some php file into another - for this are include and require are supposed:

include '/path/to/file.php';

This will be enough.



Related Topics



Leave a reply



Submit