Wrapping perl CGI program in PHP code?
Moderator: General Moderators
Wrapping perl CGI program in PHP code?
I have a PHP website and I'm using a few perl cgi scripts too. The PHP website design is pretty much standard with a title bar on the top and a navigation bar on the left.
The pages generated by the perl scripts look very different and do not fit into the rest of the website design.
Therefore I want to call the perl script from within a PHP page, collect the output, extract whatever is within the <body> tags and display that code between my navigation and title bar, just as the other PHP generated code. This way the cgi script pages would share the same look as the rest of the website.
The trick is of course to pass any CGI script parameters to the perl script and properly parse the output to extract the actual page content.
Is anybody aware of existing PHP code that does the job or do I need to write this from scratch? Any hints appreciated.
The pages generated by the perl scripts look very different and do not fit into the rest of the website design.
Therefore I want to call the perl script from within a PHP page, collect the output, extract whatever is within the <body> tags and display that code between my navigation and title bar, just as the other PHP generated code. This way the cgi script pages would share the same look as the rest of the website.
The trick is of course to pass any CGI script parameters to the perl script and properly parse the output to extract the actual page content.
Is anybody aware of existing PHP code that does the job or do I need to write this from scratch? Any hints appreciated.
Err.....
Can that CGI script be called from a command line
If it's Perl, I think we know the answer to that one.
So, err....., use shell_exec().
Now you can just parse $cgi_output and dump into your page as you see fit.
Later on,
BDKR
So, err....., use shell_exec().
Code: Select all
$cgi_output = shell_exec("your_cgi_sript.pl");Now you can just parse $cgi_output and dump into your page as you see fit.
Later on,
BDKR
Also, experiment with that script to see if it can accept command line args. If not, can it be
modified to do so? If it can, just pass the args in that shell_exec() line.
Later on,
BDKR
modified to do so? If it can, just pass the args in that shell_exec() line.
Code: Select all
$cgi_output=shell_exec("your_script.pl arg1 arg2 arg3");BDKR
No, that won't work. I can certainly call shell_exec("myscript.cgi"), but a CGI script does not process command line args. Arguments are passed to the CGI script via the QUERY_STRING environment variable (see http://hoohoo.ncsa.uiuc.edu/cgi/env.html#query), so the PHP code must set up this variable somehow. It must probably set up lots of other environment variables too.BDKR wrote:Also, experiment with that script to see if it can accept command line args. If not, can it be
modified to do so? If it can, just pass the args in that shell_exec() line.
Later on,Code: Select all
$cgi_output=shell_exec("your_script.pl arg1 arg2 arg3");
BDKR
The second problem is parsing the content of $cgi_output: It will contain a few lines of header, followed by <html><header> ... and whatnot. Now I'm only interested in the code between the <body> and </body> tag. How do I extract that piece?
I hoped that this is a common problem when moving from a CGI website to a PHP website and already has been solved....
if it's running on unix/linux try thisDoes this contain all mandatory settings?
Code: Select all
<html><body><fieldset><legend>before</legend><pre><?php
system('set'); ?></pre>
</fieldset><fieldset><legend>after</legend><pre><?php
$command = '';
foreach($_SERVER as $key=>$value)
$command .= "$key="$value"; ";
$command .= 'set ';
system($command);
?></pre></fieldset></body></html>yes this will probably work. Thanks alot. The variables I need are in $HTTP_SERVER_VARS, not in $_SERVER though.
Now the final missing piece is the parsing and extracting of the HTML code in the <body> tag. Do you have any ideas for this problem too?
Will post the PHP/CGI wrapper here when it is done.
Now the final missing piece is the parsing and extracting of the HTML code in the <body> tag. Do you have any ideas for this problem too?
Will post the PHP/CGI wrapper here when it is done.
so you have an php-version 4.1 ?!
anyway.
before calling the cgi-script start output-buffering (ob_start()) and after the execution grab the stored contents (ob_get_contents and ob_end_clean)
You may use PCRE to get the body-contents, i.e.it should be something like '!(?<=<body>).*(?=</body>)!'; but I didn't get this to work and have to leave now 
to see what I was trying to do take a look at PCRE#assertions
anyway.
before calling the cgi-script start output-buffering (ob_start()) and after the execution grab the stored contents (ob_get_contents and ob_end_clean)
You may use PCRE to get the body-contents, i.e.
Code: Select all
$pattern = '!<body>.*(?=</body>)!';to see what I was trying to do take a look at PCRE#assertions