Forum Moderators: coopster

Message Too Old, No Replies

PHP file executes another PHP file, which executes a CGI script

This must be possible, but I can't make it work.

         

MatthewHSE

4:56 pm on Dec 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As the subject line says, I have a PHP file that I need to execute another PHP file, which in turn is executing a Perl script. I need to pass the HTML output of the Perl script clear back to the initial PHP file. (Yes, it's bulky, but I seriously doubt anything else will work except a http include() and I don't want to do that.)

Anyway, when called directly from the browser, the Perl script gives HTML output. So that step is okay.

The "middle man," or the second PHP script in the description above, has the following code:

<?php
putenv('REQUEST_METHOD=POST');
putenv('QUERY_STRING=?id=5');
exec('/path/to/perlscript.cgi', $output);
array_shift($output);
echo(join('', $output));
?>

When calling this script directly from the browser, it gives the very same HTML output that the initial Perl script gave, which is also the desired result.

My problem is getting the first PHP script to run the PHP script above. Here's what I have in the first PHP file, which is really the only one that will ever be accessed by my visitors:

<?php
putenv('REQUEST_METHOD=POST');
exec('/path/to/phpscript.php', $output);
echo(join('', $output));
?>

When calling this script from a browser, all it returns is an error message: "Could not open input file:" That's all it says; I would have expected more but it doesn't even tell what file it's looking for.

I know this is a very roundabout way of accomplishing something, but it's a strange situation and it has to work this way. (I can explain why if that will help, but it gets confusing.) So how can I change that last code snippet so it will actually run the second PHP script and return the HTML from the Perl script at the end of the line?

Thanks in advance,

Matthew

mm1220

5:06 pm on Dec 27, 2005 (gmt 0)

10+ Year Member



I would imagine that you could just include the other php script in the first one.

include('/path/to/phpscript.php') instead of exec.

MatthewHSE

5:22 pm on Dec 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that would normally work, but not in this case. The whole reason this is so complicated is that the perl script must be executed from a certain directory due to its dependencies on other perl scripts. The PHP script I'll be presenting to my visitors needs to include the HTML output of this Perl script, but it also must be accessed from a different directory than the one that contains the Perl script. I have the "middle man" in the same directory with the Perl script, so it runs it with no problems. But using include() just pulls in the second PHP script into the first, which means the Perl script is actually being executed from a different directory than its own. This causes a software error. So I really do need to use exec or some similar function.

mm1220

8:28 pm on Dec 27, 2005 (gmt 0)

10+ Year Member



Okay, in that case I'd get to your unix command line and run 'whereis php' to find out where php is located on the server (assuming its cgi and not an apache module) and then change then exec line to something:

exec('/path/to/php.exe /path/to/phpscript.php', $output);