Solved

Pass PHP Script Parameters to Perl Script Called from Within PHP

Posted on 2006-11-19
5
398 Views
Last Modified: 2009-07-29
On a Unix server, I can call a Perl script from within a PHP script, with:
<?php
system("/usr/www/users/myuser/cgi-bin/perscript.cgi");
 ?>
      However, this particular Perl script requires variables as GET parameters to be passed to it from the PHP script, like:  www.example.com/phpscript.php?var=1.23  --> usr/www/users/myuser/cgi-bin/perlscript.cgi?var=1.23 .
   I tried to do it with
<?php
$var = $_GET['var'];
system("/usr/www/users/myuser/cgi-bin/perlscript.cgi?var=$var");
 ?>
but that did not work; the parameter is not getting passed to the Perl script.
I also tried:  system("/usr/www/users/myuser/cgi-bin/perscript.cgi $var");
but that did not work either.
    Strictly as a test, I even tried hard-coding the value, like
              system("/usr/www/users/myuser/cgi-bin/perscript?var=1.23");
but even that does not work to pass the parameter to the Perl script.
    What is the right syntax to use in PHP?
0
Comment
Question by:Randall-B
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:ygoutham
ID: 17977440
try using wget instead.

when you do a wget the system gets the script output through the web engine.

i do a

"wget http://localhost.com/cgi-bin/script.cgi?var=123"

when you run this command inside php you can get the output of the cgi file with the parameters passed through as though you were requesting a webpage with parameters passed.  when you add this to a crontab or someother automated place, note that the script file is downloaded to the home directory (or root if crontab is used).  so a clean up of the file after running should also be considered.

i run a weget and at the end of the script i also do a delete of

rm -f ~/script.cgi?*

this leaves no trace of the script running but the output could be used for whatever purposes.

good luck
0
 

Author Comment

by:Randall-B
ID: 17977484
ygoutham,
   Thanks, but I don't think wget is quite what I'm looking for. I think the "virtual" function works better for what I am trying to do: the user should type  phpscript.php?var=1.23 , but  perlscript.cgi?var=1.23 runs, and the user never notices that a Perl script is involved, because the user has added the ?var=1.23 argument onto the PHP script's name and only sees the PHP script's name in the URL.  The "virtual" function  works fine if the argument is hard-coded like:
   <?php
     virtual("/cgi-bin/perlscript.cgi?var=1.23");
     ?>
But the problem is: it does not properly pass the argument to Perl if the argument is specified by a variable like this:
   <?php
     $var = $_GET['var'];
     virtual("/cgi-bin/perlscript.cgi?var=$var");
     ?>
So the "virtual" function gets me halfway there, but I need to make it pass the PHP url arguments to the called Perl script. Any suggestion?
0
 
LVL 14

Expert Comment

by:ygoutham
ID: 17977525
you can run a system command in php with


exec("wget http://localhost/cgi-bin/perl-script.cgi?var=1.23", $output, $return);

OR
$varvalue = 1.23;
exec("wget http://localhost/cgi-bin/perl-script.cgi?var=$varvalue", $output, $return);


this gives you an array in $output which can be parsed as single elements.  this also gives you the scope of pasing URL based arguments to the function that you want called in the *.cgi file.  the entire file should now be available in $output

foreach($output as $line){print "$line<br>";}

what i meant is only an idea of passing url based arguments.  be it in perl or in php. either of the cases should work fine as long as we use wget and a url based parameter to pass onto wget.
0
 

Author Comment

by:Randall-B
ID: 17977527
I think I figured it out. The following code works fine:

    <?php
     $var = $_GET['var'];
     $arg = "/cgi-bin/perlscript.cgi?var=$var";
     virtual($arg);
    ?>

And, on further tries, even the following works fine:

   <?php
     $var = $_GET['var'];
     virtual("/cgi-bin/perlscript.cgi?var=$var");
   ?>

I like this, as it is transparent to the user, who never sees anything but a PHP script (yet, a Perl script is actually providing the functionality).
0
 
LVL 14

Accepted Solution

by:
ygoutham earned 500 total points
ID: 17977551
good.  sometimes bouncing your idea a little bit further lets you evolve your own solutions.  it is not necessary that every solution provided is "the" solution, but only a working variant.  there are multiple solutions.  have fun :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question