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

I'm writing what should be a dirt simple perl scipt for a friends site and I've hit a wall in dealing with his php layout.
He has header.php and footer.php that I need to be able to exec and use the output of in my perl script. I of course tried
$header = `php -q /var/www/html/testing/header.php`;
Now this works fine from the command line, but once it gets to apache it just dumps all the whole program as plain html to the browser without executing it. And it doesn't exec the php file either.

I never cared for php and I'm begining to really hate it now. Any suggestions?

Also, I can't use PHP::Include because I'm running perl v5.6 and MCPAN says it needs 5.8
  • Comment on An unfortunate incounter with PHP (loading php output into a perl variable)
  • Download Code

Replies are listed 'Best First'.
Re: An unfortunate incounter with PHP (loading php output into a perl variable)
by demerphq (Chancellor) on Apr 30, 2003 at 23:39 UTC

    I've never looked into it, but a few times I've had situations where the behaviour of backticks wasnt as I suspected. Have you tried something like

    my $header=do { open my $php,'php -q /var/www/html/testing/header.php |' or die "PHP puked: $!"; local $/; <$php> };

    Which although untested should do the same thing, but might work. Also try doing it via the shell explicitly. For instance on Win2k I might do "cmd.exe /c php ...". Anyway, usually when backticks havent done what I thought it should, one of the others has. :-)


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: An unfortunate incounter with PHP (loading php output into a perl variable)
by gmpassos (Priest) on May 01, 2003 at 04:39 UTC
    Use Open3, that catch all the output:
    use IPC::Open3 ; open3(LOGREAD , LOGERROR, LOGWRITE, "php -q /var/www/html/testing/he +ader.php"); @data = <LOGREAD> ; @error <LOGERROR> ; close(LOGREAD , LOGERROR, LOGWRITE) ; print join("", @data) ;
    Open3 is good, since is the more portable option to do that, and you have the write handler too to send data!

    Graciliano M. P.
    "The creativity is the expression of the liberty".

      Same deal. It executes fine at the command line and craps out through apache.

      Interesting side note: I tried writing a tempfile with the parsed php data in it and it still didn't work. So I looked at the temp file and it was a mirror of my perl script with "Content-type..." at the top of it.
        What?! Strange!

        Well, try php -q script.php >out.htm

        Ugly, but should work!

        If doen't work you can try to get the PHP output from http://foo/script.php, using any LWP or making a simple IO::Socket::INET. But this is ugly too!

        Graciliano M. P.
        "The creativity is the expression of the liberty".

Re: An unfortunate incounter with PHP (loading php output into a perl variable)
by crenz (Priest) on Apr 30, 2003 at 22:57 UTC

    once it gets to apache it just dumps all the whole program as plain html to the browser without executing it

    This actually sounds more like a problem with your webserver configuration. While calling another process might be a problem, it would show in a different way.

    Are you sure you are able to execute any CGI scripts on your server? If no, try a simple test program first and fix your server configuration.

      Yeah, the program executes fine so long as I comment out the header and footer lines.
Re: An unfortunate incounter with PHP (loading php output into a perl variable)
by nite_man (Deacon) on May 02, 2003 at 07:01 UTC
    In my mind, redevelop PHP header and footer scripts on Perl or your Perl script on PHP will be better than using mix of PHP and Perl scripts together. I think that server side should be clearly and mix of different technology is not good choise.
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
      Yeah, I'm coming to relize this. And I've been reading up on PHP in hopes that I could just rewrite the script. I can't really ask the guy to redesign his site so my script will work right even tough everything just tastes better made with perl.