Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

Pass PHP Script Parameters to Perl Script Called from Within PHP

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
Randall-B
Asked:
Randall-B
  • 3
  • 2
1 Solution
 
ygouthamCommented:
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
 
Randall-BAuthor Commented:
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
 
ygouthamCommented:
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
 
Randall-BAuthor Commented:
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
 
ygouthamCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now