Talking to PHP from Perl

petdance on 2004-05-28T21:10:16

I'm looking to do some Perl code that can access PHP code, so that we don't have to rewrite the working PHP code in Perl. It looks like we can pass stuff around via the Apache notes table. Here's what I've done as proof of concept: package TW::Apache::Play;

use strict; use warnings; use Apache::Request; use Apache::Constants qw( :common );

sub handler { my $r = Apache::Request->new( shift );

my $username = $r->param('username') || "alester";

$r->send_http_header( "text/html" );

print ""; print "

Looking up \"$username\" via PHP

"; my $sub = $r->lookup_uri( "/foo.php" ); $sub->notes->set( username => $username ); my $rc = $sub->run(); my $notes = $sub->notes; print "custid=$notes->{custid}
"; print "contact=$notes->{contact}
"; print "All done!"; print ""; return OK; } 1; This mod_perl handler fetches a parm from the URL, and passes it into the notes table for a subrequest. Then, it executes that subrequest, which is the following PHP file:

$username ) );

apache_note( "custid", $cols["CUSTID"] ); apache_note( "contact", $cols["CONTACT"] ); ?>


The PHP gets the username note from Apache, does a lookup in the database, and then passes back two columns in the notes table. Control is the passed back to the mod_perl handler, which extracts those two columns from the notes table and displays them on the screen.



Of course, in real life, the PHP code will be calls to some hairy business logic that is thoroughly debugged that I don't want to rewrite on the Perl side.

Is there any danger in doing this? As I understand the notes table, they're per-request only, and won't stick around after the request is completed, so I don't have to worry about children polluting each other. Surely there's a performance hit of some kind, but I assume it's only relatively small.

Any words of experience from anyone out there?