how to invoke external php script ?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


when a web request comes to my index.php, i would like index.php to execute
a php script stored in an external file and capture the external script's
output to a variable.

i am doing this because the external script can read the database and can
emit xml that i want to use. index.php (or whatever) wants to grab this xml
and pass it through an xslt.

i know that if the external php script had a function declared, and the
function returned a string value with the xml contents, then i could use
"require" or a similar construct and call the function. the question that i
am trying to answer is what to do if the external script uses echo() to
output the results? how to capture its output during request processing?
besides, i do not like spawning another process for that and piping to it.
it might not scale well. ;-)

please help.

Re: how to invoke external php script ?

Use the -q directive to suppress headers

In index.php:
$stdout_var = shell_exec(/usr/local/bin/php -q /path/to/the/php/file.php);

Make sure file.php is chmod 755 or thereof

Check the path to php (could be "sbin")

You could write $stdout_var to file or do whatever from here.

On 2/25/05 1:25 PM, in article, "konsu"

Quoted text here. Click to load it

Re: how to invoke external php script ?

Another route is:

$sResponse = `php -q -d max_execution_time=60000

In this example, I also add the "-d max_execution_time=60000" so that
your external script has a longer timeout time, in case you need that.

Note the backticks (`) in the code above -- these are not single

I also don't do a direct path to PHP. If your $PATH already contains
the PHP bin path, then you might not need it.

Also, on my system, PHP is reached with /usr/bin/php instead of
/usr/local/bin/php. (I have RH9 Linux.)

Re: how to invoke external php script ?

konsu wrote:
Quoted text here. Click to load it

Is that script located on your system or somewhere else?  If it
is local, (i.e., resides on your system), you can simply do
soomething like this:

$capture = file_get_contents('http://localhost/script.php');

If the external script is remote, you can do

$capture = file_get_contents(' ');


Site Timeline