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

Hi Monks!

For complex reasons, I need to study how Perl and PHP can interact.
The issues are to be able to call perl API form a php script or to be able to interpret php code within a perl script.
I use more perl than PHP. So first, I looked for on the CPAN and I found php::include module but it is not really convenient for my needs.

then, I found this php module . It looks quite good. has anyone tested it?
I would like to have your opinion on the feasability of these issues.

thanks for your help.

Replies are listed 'Best First'.
Re: Perl and PHP interoperability
by stevecomrie (Beadle) on Jan 13, 2005 at 15:24 UTC
    i've found myself in the same boat a number of times.

    the following bit of code has come in handy once or twice.

    my $tempfile = '/some/temp/directory/and/filename.php'; open( FILE, ">$tempfile" ); print FILE $output; close FILE; $output = `/usr/local/bin/php $tempfile` || die "Error: $!\n\n$output" +;
    that will execute php from inside a perl script ..

    edit: sorry, forgot to add, that code snippet assumes that you have an html/php page stored in $output. If you have a php page existing on your server that you just want to run, you can skip the whole output to file bit, and just use the my $output = `/usr/local/bin/php $pathtofile` bit to collect the output of that file.

    i use the above code in my perl based CMS system when i need to display a preview of a page that contains php.

    on the other hand:

    <?php echo file_get_contents( 'http://www.domain.com/cgi-bin/something.cgi?r +m=this&value=that' ); ?>
    will output the contents of a perl script executed from inside a php page.

    hope that helps.
Re: Perl and PHP interoperability
by wazoox (Prior) on Jan 13, 2005 at 15:04 UTC
    I think we'd need some examples of what you mean to do. My first move would be : why do you need to do such strange things? Can't you simple run a perl script from a php script, or run a php script from a perl script, or rewrite whatever is written in one language in the other one?
Re: Perl and PHP interoperability
by mbalex (Beadle) on Jan 13, 2005 at 18:25 UTC
    this is not the solution to the "interpret php code from perl" problem, but it still something about how those two languages can interact. I fequently use >soap< to connect php script to perl scripts. for example, a perl script is collecting system data and then sends it to the php script running on a webserver, which sorts and updates the data at the mysql server. then again i have a perl server running, which is beeing administrated by a php web gui, all over soap.
Re: Perl and PHP interoperability
by redhotpenguin (Deacon) on Jan 14, 2005 at 02:07 UTC
    Here's a great example of Perl and PHP interoperability - testing PHP with Perl. I saw Chris and Geoff's presentation of this methodology at ApacheCon 2004, it was very entertaining!
Re: Perl and PHP interoperability
by superfrink (Curate) on Jan 13, 2005 at 19:37 UTC
    I once had to make a Perl CGI read a PHP session data (stored in a file in /tmp) so I used PHP::Session. You could also use a database to store the session variables and specify a custom session handler in the PHP scripts.
Re: Perl and PHP interoperability
by hakkr (Chaplain) on Jan 13, 2005 at 17:46 UTC
    I think in Apace 2.0 you should be able to play around with piping processes before returning the page?