Wrapping perl CGI program in PHP code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nn4l
Forum Newbie
Posts: 3
Joined: Fri Aug 30, 2002 9:05 am

Wrapping perl CGI program in PHP code?

Post by nn4l »

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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Err.....

Post by BDKR »

Can that CGI script be called from a command line :oops: :?: If it's Perl, I think we know the answer to that one.

So, err....., use shell_exec().

Code: Select all

$cgi_output = shell_exec("your_cgi_sript.pl");

8O :!:

Now you can just parse $cgi_output and dump into your page as you see fit.

Later on,
BDKR
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by 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.

Code: Select all

$cgi_output=shell_exec("your_script.pl arg1 arg2 arg3");
Later on,
BDKR
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Cool, didn't know that... Thanks
nn4l
Forum Newbie
Posts: 3
Joined: Fri Aug 30, 2002 9:05 am

Post by nn4l »

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.

Code: Select all

$cgi_output=shell_exec("your_script.pl arg1 arg2 arg3");
Later on,
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.

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....
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if it's running on unix/linux try this

Code: Select all

&lt;html&gt;&lt;body&gt;&lt;fieldset&gt;&lt;legend&gt;before&lt;/legend&gt;&lt;pre&gt;&lt;?php
system('set'); ?&gt;&lt;/pre&gt;
&lt;/fieldset&gt;&lt;fieldset&gt;&lt;legend&gt;after&lt;/legend&gt;&lt;pre&gt;&lt;?php

$command = '';
foreach($_SERVER as $key=&gt;$value)
	$command .= "$key="$value"; ";
$command .= 'set ';

system($command);
?&gt;&lt;/pre&gt;&lt;/fieldset&gt;&lt;/body&gt;&lt;/html&gt;
Does this contain all mandatory settings?
nn4l
Forum Newbie
Posts: 3
Joined: Fri Aug 30, 2002 9:05 am

Post by nn4l »

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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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.

Code: Select all

$pattern = '!<body>.*(?=</body>)!';
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
Post Reply