Alatar has asked for the wisdom of the Perl Monks concerning the following question:

At the risk of being savaged by rabid coders.... is there any way of calling a PHP script from within a perl script? I've tried all the usual things like require and include, even copying the PHP tags and code into a HTML quotable part of the perl script but to no avail!

The PHP script in question returns HTML which I want to display in my perl driven portal site. However, when I try to call the PHP it gets unceremoniously dumped to the browser window without any attempt to run it.

Now I know this is not exactly ideal, but it seems preferable to me to translating the entire PHP script into Perl (even if I were capable of that!).

Any help, as usual, will be much appreciated! :-)
Alatar

Replies are listed 'Best First'.
Re: Calling a PHP script from within Perl
by diotalevi (Canon) on Apr 13, 2004 at 13:59 UTC

    Call PHP like a CGI?

    $ENV{'PATH'} = '/usr/bin'; my $stuff = `/usr/bin/php foo.php`;
Re: Calling a PHP script from within Perl
by matija (Priest) on Apr 13, 2004 at 14:06 UTC
    In a large way, this depends on your web server. As far as I know, there is no way to do it in Apache 1.x, but it is possible in Apache 2.x, where you can specify a list of handlers.

    And it means you will have more load on your server, since you'll be doing double processing, (Perl and PHP) for one pageview.

    Your best options, IMHO, are:

    • Return all the data for the page from the PHP script
    • Rewrite the script in Perl (hey, learn by doing)
    • (not recommended) Call the PHP script from your Perl script using LWP::Simple or LWP::UserAgent.
      The command line php interpreter is completely seperate from apache so if it's on the local machine, it really has nothing to do with apache at all, it will be loading both a perl, and php interpreter to run the program, but depending on the situation, that might me more desirable then duplicating the code in perl as far as code maintance and whatnot is concerned.


      daN.
Re: Calling a PHP script from within Perl
by tachyon (Chancellor) on Apr 13, 2004 at 15:48 UTC

    PHP::Include has some basic functionality. There are also a raft of PHP:: modules. However given that you have a functional PHP script running on a web server and want to get at the data from your Perl CGI code you are possibly stuck with something ugly (and slow) like:

    use LWP::Simple; my $php_html = get( http://somesite.com/blech.php ); print "Content-Type: text/html <h1>PHP is yucky</h1> $php_html <h1>Brought to you with the power of Perl</h1>";

    cheers

    tachyon