Pass PHP Script Parameters to Perl Script Called from Within PHP

Posted on 2006-11-19
Last Modified: 2009-07-29
On a Unix server, I can call a Perl script from within a PHP script, with:
      However, this particular Perl script requires variables as GET parameters to be passed to it from the PHP script, like:  --> usr/www/users/myuser/cgi-bin/perlscript.cgi?var=1.23 .
   I tried to do it with
$var = $_GET['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
but even that does not work to pass the parameter to the Perl script.
    What is the right syntax to use in PHP?
Question by:Randall-B
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 14

Expert Comment

ID: 17977440
try using wget instead.

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

i do a


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

Author Comment

ID: 17977484
   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:
But the problem is: it does not properly pass the argument to Perl if the argument is specified by a variable like this:
     $var = $_GET['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?
LVL 14

Expert Comment

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);

$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.

Author Comment

ID: 17977527
I think I figured it out. The following code works fine:

     $var = $_GET['var'];
     $arg = "/cgi-bin/perlscript.cgi?var=$var";

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

     $var = $_GET['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).
LVL 14

Accepted Solution

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 :)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Curl Problem 10 77
CSV export single column not inserting comma's in file 11 69
How to close my php post page. 3 42
How can I send attachment with mail 5 26
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

752 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